【问题标题】:ajax call to asp web servers gives is not allowed by Access-Control-Allow-Origin. errorAccess-Control-Allow-Origin 不允许对 asp Web 服务器进行 ajax 调用。错误
【发布时间】:2013-08-18 06:16:51
【问题描述】:

我创建了一个对我的本地主机网络服务器进行 ajax 调用的函数

我从控制台收到此错误消息。 Access-Control-Allow-Origin 不允许 Origin。 这是我的 web.config

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
      </customHeaders>
    </httpProtocol>

  </system.webServer>

这是我的 JS 函数:

function doAjax()
{
$.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "http://localhost:5181/WebService1.asmx/UpdateReport",

     data: "{'test_description':'" + TestCaseDiscription + "', "
           + "'test_id':'" + id + "',"
           + "'tester_name':'" + TesterName + "',"
           + "'test_url':'" + test_url + "'}",

    dataType: "json",
    error: function (err){ 
                alert(err)
        },

    success: function (data){
                    alert(data)
    }

});
}

这是我的网络服务

[WebMethod]
public string UpdateReport(string test_description, string test_id, string test_url, string tester_name)
{
    report report = new report();

    report.test_description = test_description;
    report.test_id = test_id;
    report.test_url = test_url;
    report.tester_name = tester_name;


    if (report.UpdateDataBase())
        return "correct";

    return "Sorry";
}

任何想法为什么它不起作用? 我一直在读到这个问题出现是因为跨域 ajax 调用。 但我不知道如何解决它。

有什么想法吗?

【问题讨论】:

    标签: asp.net jquery cross-domain


    【解决方案1】:

    Access-Control-Allow-Origin 不是服务器端问题,而是客户端问题。您不能调用来自不同域的内容...包括不同的安全级别,即 ssl 或子域或套接字!。

    不过有一个解决方法,JSONP。这允许您调用脚本以作为回调函数执行。来自服务器的响应需要包含在请求中发送的回调函数中,或者如果您使用 jquery,它会为您处理脏活。看例子

           `jQuery.ajax({
            dataType: 'jsonp',
            data: payload,
            url: "http://someotherdomain.com?callback=?",
            jsonp: true})
            .done(function (response, status, request) {
                if (response.success)
                    processResponse(response.success);
                console.log("done");
            })
            .fail(function (response, status, request) {
                console.log("fail");
    
            })
            .always(function (response) {
                console.log("always");
    
            })`
    

    服务器端php

    $response = array("key" => 'A Message');
    $success = $success?"success":"failure";
    $response = json_encode($response);
    echo $_GET['callback'].'(' . "{\"".$success ."\" : ".$response."}" . ')';
    

    【讨论】:

    • 感谢您的回复,那么您说我不能像这样进行 ajax 调用是什么意思?只能通过回调?
    • [link]en.wikipedia.org/wiki/JSONP[/link] 是的。然而,使用这种方法你可以做很多事情。 [链接]en.wikipedia.org/wiki/Same-origin_policy[/link]
    • 我仍然被阻止,可能是因为该网站是 https?
    • JSONP 的关键组件之一是回调=?在查询字符串中,你记得这样做吗?你在做一个 JSONP 调用吗?您是否仍在尝试对服务器进行常规 ajax 回调?它们在所有帐户上都匹配吗? http、端口、域
    • 我已将数据类型更改为 JSONP 并将类型更改为 Get,现在它正在运行!!!!但它禁用了网站上的 https。你知道为什么吗?非常感谢!!!!
    猜你喜欢
    • 2013-03-10
    • 2019-11-25
    • 2015-06-17
    • 2013-07-17
    • 2013-09-09
    • 2013-08-29
    • 2012-03-08
    • 2013-05-16
    相关资源
    最近更新 更多