【发布时间】:2014-10-16 06:04:44
【问题描述】:
我正在尝试使用 POST 请求对我的用户进行身份验证。但是当我通过 jQuery 调用 rest 调用时,它会进入错误状态并返回错误代码 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
}
}
}
对此有什么想法吗?提前致谢!!
【问题讨论】:
-
您正在尝试发送跨域 ajax 请求并且目标资源不支持 CORS,这是问题
-
您的浏览器控制台应该显示类似
XMLHttpRequest cannot load https://appserver.dev.cloud.wso2.com/t/madusanka/webapps/projecttracker-default-SNAPSHOT/services/projecttracker/userservice/users/authenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.的错误