【问题标题】:Cross domain call error : XMLHttpRequest: Network Error 0x80070005, Access is denied跨域调用错误:XMLHttpRequest:网络错误 0x80070005,访问被拒绝
【发布时间】:2018-04-05 23:44:57
【问题描述】:

使用 MVC Asp.Net

以下代码适用于实际的用户名和密码。但我不想使用它。 :

WebClient client1 = new WebClient();
client1.Credentials = new NetworkCredential("actualUserName", "actualPassword");
string code1 = client1.DownloadString(@"http://Domainname/GetValue");

尝试了javascript方法:

 $.ajax({
    type: 'POST',
    url: Url,
    processData: false,
    contentType: 'application/json; charset=utf-8',
    dataType: 'jsonp',
    crossDomain: true,
    jsonpCallback: 'CallBackMethod',
    success: function (json) {
        console.log(json); 
    },
    error: function (xhr, textStatus, errorThrown) {
        console.log(errorThrown);
    }
});

但出现以下错误:

Multiple Access-Control-Allow-Origin headers are not allowed for CORS response.
XMLHttpRequest: Network Error 0x80070005, Access is denied.

尝试在 Application_BeginRequest 中的 Global.asax 中添加标头,并在 webconfig 中添加类似条目:

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
    HttpContext.Current.Response.End();
}

以上方法都试过了,在Win7机器上不行。但在Win10机器上工作!!还尝试在服务器中部署并检查,但弹出相同的错误。

【问题讨论】:

    标签: javascript asp.net-mvc xmlhttprequest cors networkcredentials


    【解决方案1】:

    问题的关键就在错误信息中:

    不允许使用多个 Access-Control-Allow-Origin 标头

    服务器以重复的标头响应。这导致一些浏览器失败。您提到您在两个地方实施了 CORS。这是一个很好的起点。删除一个实现,看看会发生什么。

    也有一些框架可以实现 CORS。最重要的是,您只需要一个,而且效果最好的一个。无论您保留什么实现,它都需要处理标准请求和预检请求 (OPTIONS)。

    【讨论】:

      【解决方案2】:

      以下已解决问题:

      IIS -> 身份验证 -> Windows 身份验证 -> 提供程序 -> 删除协商

      【讨论】:

        猜你喜欢
        • 2015-10-17
        • 2018-03-11
        • 2017-08-11
        • 2015-01-31
        • 2019-09-12
        • 2018-09-07
        • 1970-01-01
        • 2017-06-08
        • 2015-09-03
        相关资源
        最近更新 更多