2017-03-16 3 views
0

Während den folgenden Code verwenden, erhalte ich

400 Bad Anfrage

$scope.signUp = function() { 
 
    var data = { 
 
    \t email: $scope.email, 
 
    \t password: $scope.password, 
 
    \t first_name: $scope.fName, 
 
    \t last_name: $scope.lName, 
 
    \t username: $scope.uName, 
 
    \t billing: { 
 
    \t \t first_name: $scope.fName, 
 
    \t \t last_name: $scope.lName, 
 
    \t \t company: $scope.cName, 
 
    \t \t address_1: $scope.address1, 
 
    \t \t address_2: $scope.address2, 
 
    \t \t city: $scope.city, 
 
    \t \t state: $scope.state, 
 
    \t \t postcode: $scope.pcode, 
 
    \t \t country: $scope.country, 
 
    \t \t email: $scope.email, 
 
    \t \t phone: $scope.mobile, 
 
    \t }, 
 
    \t shipping: { 
 
    \t \t first_name: $scope.fName1, 
 
    \t \t last_name: $scope.lName1, 
 
    \t \t company: $scope.cName1, 
 
    \t \t address_1: $scope.address11, 
 
    \t \t address_2: $scope.address12, 
 
    \t \t city: $scope.city1, 
 
    \t \t state: $scope.state1, 
 
    \t \t postcode: $scope.pcode1, 
 
    \t \t country: $scope.country1, 
 
    \t } 
 
    } 
 

 
    console.log(data) 
 
    $http.post("https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers", { 
 
    \t \t headers: { 
 
    \t \t \t 'Content-Type': 'application/json', 
 
    \t \t \t 'Authorization': 'Basic ' + window.btoa("username:password") 
 
    \t \t }, 
 
    \t \t data: data 
 
    \t }) 
 
    \t .then(function (response) { 
 
    \t \t console.log(response) 
 
    \t }, function (response) { 
 
    \t \t console.log(response); 
 
    \t }); 
 
}

Aber wenn ich die folgende verwenden rest_missing_callback_params Code, es sendet die Daten auf dem Server.

var au = window.btoa("username:password"), 
 
    req = { 
 
    \t method: 'POST', 
 
    \t url: 'https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers', 
 
    \t headers: { 
 
    \t \t 'Content-Type': 'application/json', 
 
    \t \t 'Authorization': 'Basic ' + au 
 
    \t }, 
 
    \t data: data 
 
    } 
 

 
$http(req).then(function (response) { 
 
\t console.log(response) 
 
}, function (response) { 
 
\t console.log(response); 
 
});

Was zwischen diesen beiden ist der Unterschied? Warum passiert es so?

+0

Überprüfen Sie die Netzwerkkarte des Entwicklertools ... sehen, was in jeder Anfrage gesendet wird, wenn Sie den Unterschied erkennen, haben Sie Ihre Antwort –

+0

Obwohl iam Senden von E-Mail und Passwort gibt es den Fehler fehlende E-Mail und Passwort –

+0

so gibt es absolut keinen Unterschied in den Anfrage-Kopfzeilen oder den Post-Parametern? –

Antwort

1

Für die oben Beispiel arbeiten Sie dies ändern müssen:

$http.post("https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers", { 
    headers: { 
    'Content-Type': 'application/json', 
    'Authorization': 'Basic ' + window.btoa("username:password") 
    }, 
    data: data 
}) 

Um dies:

$http.post("https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers", data, { 
    headers: { 
    'Content-Type': 'application/json', 
    'Authorization': 'Basic ' + window.btoa("username:password") 
    } 
}) 

Nach dem die Winkel $http docs (https://docs.angularjs.org/api/ng/service/ $ http # post) $http.post() hat eine andere Methodensignatur (post(url, data, [config]);) als $http() ($http(config)).