2017-03-16 59 views
0

雖然使用下面的代碼,我得到

400錯誤的請求rest_missing_callback_params

$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 }); 
 
}

但是當我用下面的代碼,它將數據發佈到服務器。

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); 
 
});

的是這兩者之間的區別?爲什麼會發生這種情況?

+0

檢查開發者工具的網絡標籤...看到什麼是在每個請求發送,如果你看出其中的區別,你有你的答案 –

+0

儘管IAM發送電子郵件和密碼,它提供了錯誤丟失的電子郵件和密碼 –

+0

等等,請求頭或帖子參數完全沒有區別? –

回答

1

爲頂級例如工作,你需要改變這一點:

$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 
}) 

要這樣:

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

根據該角$http文檔(https://docs.angularjs.org/api/ng/service/ $ HTTP#後)$http.post()有不同方法簽名(post(url, data, [config]);)比$http()$http(config))。