【问题标题】:Error while calling to post rest from jquery从jquery调用发布休息时出错
【发布时间】: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. 的错误

标签: jquery json rest


【解决方案1】:
  • 确保您的域来源在位于appserver.dev.cloud.wso2.com 的 rest 服务服务器中被允许
  • 全局设置jQuery.support.cors = true;
  • 将 ajax 之前的 dataType js var 更改为 var dataType="json";
  • setHeaders 如果需要 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多