我正在尝试使用POST请求对我的用户进行身份验证。但是当我通过jQuery调用其余的调用时,它会进入错误状态并返回错误代码0。
当我使用邮递员或休息客户端呼叫同一个休息呼叫时,它会按预期为我提供输出。
我的jQuery在下面,
$(document).ready(function() {
alert('started');
var dataType="application/json";
var data = {userName:"admin",password:"admin"};
$.ajax({
type: "POST",
url: "https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate",
data: data,
async : false,
success: function(){alert("success")},
error: function(){alert("error")},
complete: function(){alert("complete")},
statusCode: {
0: function() { alert( "0 : What happened here" )},
404: function() { alert( "404 : Page not found" )},
500: function() { alert( "500 : Internal server error" )}
},
dataType: dataType
}).then(function(data) {
//handle the user here
});
});
我不知道为什么会返回http错误0。 我使用userName = admin& password = admin拨打https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate并获得以下输出。
{
Users: {
User: {
userName: "admin"
userRoleId: 13268
}
}
}
有关于此的任何想法吗?在此先感谢。!!
答案 0 :(得分:1)
appserver.dev.cloud.wso2.com
jQuery.support.cors = true;
var dataType="json";
headers
ajax属性现在你的脚本看起来像:
$(document).ready(function () {
$.support.cors = true;
var dataObject = { userName: "admin", password: "admin" };
$.ajax({
type: "POST",
url: "https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate",
data: dataObject,
async: false,
success: function () { alert("success") },
error: function () { alert("error") },
complete: function () { alert("complete") },
statusCode: {
0: function () { alert("0 : What happened here") },
404: function () { alert("404 : Page not found") },
500: function () { alert("500 : Internal server error") }
},
dataType: "json"
, headers: { SecurityToken: securityTokenValue } // or set any header info here.
}).then(function (data) {
//handle other logic
});
});
另请查看其他类似帖子:
How to get a cross-origin resource sharing (CORS) post request working