Access-Control-Allow-Origin不允许使用Origin http://example.com

时间:2012-09-27 13:05:51

标签: jquery asp.net ajax

我试图从给定的ajax调用中调用webservice方法。我的Web服务托管在同一个应用程序中。

$.ajax({
    type: "POST",
    url: "http://1.1.1.1/demo/sblead.asmx/SBLeadsSave",
    data: "{'whenNeeded':'" + whenNeeded + "','howLong': '" + howLong + "','size': '" + Size + "','customerName': '" + name + "','mobile': '" + mobile + "','email': '" + email + "','comments': '" + comments + "','nextContract': '','unitLocation': '" + unitLocation + "'}",
    contentType: "application/json; charset=utf-8",

    dataType: "jsonp",
    callback: '?',
    crossDomain: true,

    success: function (response) {
        // debugger;
        var res = response.d;
        if (res == "True") {
            location.href = "http://1.1.1.1/SBleadSuccess.htm";
        } else {
            alert('Data Saving failed please try again');
        }
    },
    failure: function (result) {
        alert(result.status + ' ' + result.statusText);
    },
    beforeSend: setHeader

当我使用Chrome开发者工具看到它时,没有调用Web服务方法 - >网络我可以看到响应,因为Access-Control-Allow-Origin不允许原始http://www.example.com

它在测试服务器中工作,但不在生产服务器中工作。

我如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您需要在响应标头中允许生产服务器:

Access-Control-Allow-Origin: <production origin> | *

您可以在MDN: HTTP access control (CORS)上看到更多信息。

答案 1 :(得分:0)

这是因为您正在调用http://1.1.1.1/demo/sblead.asmx/SBLeadsSave,但这不是您的生产服务器位置。

放置您的生产服务器位置,或只是/demo/sblead.asmx/SBLeadsSave