【问题标题】:400 Bad Request for $http post method$http post 方法的 400 错误请求
【发布时间】:2017-04-05 12:22:42
【问题描述】:

使用以下代码时,我得到了

400 错误请求 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);
});

这两者有什么区别?为什么会这样?

【问题讨论】:

  • 检查开发者工具网络选项卡...查看每个请求中发送的内容,如果发现差异,您将得到答案
  • 即使我正在发送电子邮件和密码,它也会给出缺少电子邮件和密码的错误
  • 那么,请求头或post参数绝对没有区别?

标签: javascript angularjs


【解决方案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")
  }
})

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多