2016-04-24 5 views
0

Bin neu in eckigen js. (Eine Woche alt). Ich habe eine Anforderung, die $ http verwendet, um eine Anfrage zu senden. Ich will den Fehler anzeigen, der in der Antwortnachricht auf einer anderen Seite empfangen wird, aber bin nicht in der Lage, herauszufinden, wie man den Wert einer anderen Seite übergibt Die neue Seite unterscheidet sich von der, die HTTP-Aufrufe macht. Nachstehend ist der Code, den ich geschrieben habe. Nichts scheint zu funktionieren. Bitte führen Sie mich auf, wie man mit dieser Anforderung umgehen soll?Übergeben Sie die Fehlermeldung JSON an eine andere Seitenvorlage

$http({ 
      method : 'POST', 
      url  : 'http://localhost:8080/abc/users', 
      data  : JSON.stringify(toPass) 
     }).then(function (data) { 
      // this callback will be called asynchronously 
      // when the response is available 
      console.log(data.data+'a'); 
      $scope.result="The request has been successfully submitted" +data.data.emailId; 
      $scope.x=data.data.emailId; 
      console.log('result'+x); 
      console.log('result'); 
      //$location.path('/Success?{error1}'); 
      window.location="/Success?{x}"; 
      /*if (data.data.error){ 
       $scope.error=data.data.error 
      }else{ 
       $scope.result=data.data.emailId; 
      }*/ 
      }, function (error) { 
      // called asynchronously if an error occurs 
      // or server returns response with an error status. 
      console.log("An error encountered"+error); 

      var error1=error.status; 

     // $location.path('/Success?{error1}'); 

      }); 
    } 
}) 

Antwort

0

Make Dienstleistungen wie diese

app.service(
      "friendService", 
      function($http, $q) { 
       // Return public API. 
       return({ 
        addFriend: addFriend, 
        getFriends: getFriends, 
        removeFriend: removeFriend 
       }); 
       // --- 
       // PUBLIC METHODS. 
       // --- 
       // I add a friend with the given name to the remote collection. 
       function addFriend(name) { 
        var request = $http({ 
         method: "post", 
         url: "api/index.cfm", 
         params: { 
          action: "add" 
         }, 
         data: { 
          name: name 
         } 
        }); 
        return(request.then(handleSuccess, handleError)); 
       } 
       // I get all of the friends in the remote collection. 
       function getFriends() { 
        var request = $http({ 
         method: "get", 
         url: "api/index.cfm", 
         params: { 
          action: "get" 
         } 
        }); 
        return(request.then(handleSuccess, handleError)); 
       } 
       // I remove the friend with the given ID from the remote collection. 
       function removeFriend(id) { 
        var request = $http({ 
         method: "delete", 
         url: "api/index.cfm", 
         params: { 
          action: "delete" 
         }, 
         data: { 
          id: id 
         } 
        }); 
        return(request.then(handleSuccess, handleError)); 
       } 
       // --- 
       // PRIVATE METHODS. 
       // --- 
       // I transform the error response, unwrapping the application dta from 
       // the API response payload. 
       function handleError(response) { 
        // The API response from the server should be returned in a 
        // nomralized format. However, if the request was not handled by the 
        // server (or what not handles properly - ex. server error), then we 
        // may have to normalize it on our end, as best we can. 
        if (
         ! angular.isObject(response.data) || 
         ! response.data.message 
         ) { 
         return($q.reject("An unknown error occurred.")); 
        } 
        // Otherwise, use expected error message. 
        return($q.reject(response.data.message)); 
       } 
       // I transform the successful response, unwrapping the application data 
       // from the API response payload. 
       function handleSuccess(response) { 
        return(response.data); 
       } 
      } 
     ); 
+0

Vielen Dank für Ihre Hilfe Wasiq. Ich habe Ihren Code als Basis verwendet und einen Service erstellt. – Batman

0

Gemeinsame Nutzung von Daten über Controller können entweder durch eine gemeinsame übergeordnete Steuerung oder (imho besser) durchgeführt werden, indem einen Service mit dem Sie die Daten speichern. Dienste sind Singletons in AngularJs und passen gut dazu.

Verwandte Themen