AJAX调用得到了一个" 400错误请求"来自我的REST API的响应。这是JavaScript:
var client_id = 'xxxxxx';
var client_secret = 'yyyyyy';
var username = document.getElementById("inputUsername").value;
var password = document.getElementById("inputPassword").value;
var params = {
client_id: client_id,
client_secret: client_secret,
grant_type: 'password',
username: username,
password: password
};
var successCallback = function(data, textStatus, jqXHR) {
console.log('Success: ', textStatus);
};
var errorCallback = function(jqXHR, textStatus, errorThrown) {
console.log('Error: ', jqXHR.status, textStatus, errorThrown);
};
var completeCallback = function(jqXHR, textStatus) {
console.log('Complete: ', textStatus);
};
$.ajax({
traditional: true,
type: 'POST',
url: 'http://127.0.0.1:8000/auth/token',
contentType: 'application/json',
dataType: 'json',
data: params,
success: successCallback,
error: errorCallback,
complete: completeCallback
});
我也尝试使用以下方法设置数据,但我得到了相同的响应:
var data = jQuery.param(params);
...对ajax调用的相应更改:
$.ajax({
...
data: data,
...
});
我希望获得与使用curl命令相同的结果:
curl -X POST "http://127.0.0.1:8000/auth/token" -d "client_id=xxxxxx&client_secret=yyyyyy&grant_type=password&username=aaaaaaa&password=bbbbbb"
答案 0 :(得分:2)
我删除了AJAX调用中的contentType参数,问题就消失了:
$_POST