400错误的请求为$ HTTP POST方法

问题描述:

虽然使用下面的代码,我得到

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

等等,请求头或帖子参数完全没有区别? –

为*例如工作,你需要改变这一点:

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