gpt4 book ai didi

javascript - $http post 方法的 400 错误请求

转载 作者:行者123 更新时间:2023-12-03 04:47:06 28 4
gpt4 key购买 nike

使用以下代码时,我得到

400 Bad request rest_missing_callback_params

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

console.log(data)
$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
})
.then(function (response) {
console.log(response)
}, function (response) {
console.log(response);
});
}

但是当我使用以下代码时,它将数据发送到服务器。

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

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

这两者有什么区别?为什么会出现这样的情况?

最佳答案

要使最上面的示例正常工作,您需要更改此设置:

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

根据 Angular $http 文档 (https://docs.angularjs.org/api/ng/service/ $http#post) $http.post() 有一个不同的方法签名 (post( url, data, [config]);) 优于 $http() ($http(config))。

关于javascript - $http post 方法的 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42825616/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com