【问题标题】:Not able to make an ajax call by CROS Request无法通过 CROS 请求进行 ajax 调用
【发布时间】:2015-09-29 08:17:51
【问题描述】:

我在发出 CROS 请求时遇到错误。如果我缺少任何其他设置,任何人都可以告诉我吗?

控制台错误:加载资源失败:服务器响应状态为 401(未授权)

在本地主机应用程序的 web.config 上完成的设置。

<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Content-Allow-Headers" value="Content-Type" />                 
<add name="Access-Control-Allow-Origin" value="*" />

我的 Ajax 调用

var favoriteWebPartFeedsUrl = 'http://localhost/locator.aspx?name=Common.History.Case&RenderAs=feed&ContentType=xml'   
$.ajax({
    type: "GET",    
    url:favoriteWebPartFeedsUrl,
    dataType: "json",   
    processdata: true,
    crossDomain: true,
    xhrFields: {
    withCredentials: true
    },
    success: function(response) {
        console.log("Favorite webpart is loaded", response);;
    },
    error: function (e) {                
        console.log('Featching Favorite webpart FAILED');
    }   
});

【问题讨论】:

  • 401(未经授权) - 当请求的资源落后于身份验证/登录机制时会出现此类错误。尝试将http://localhost/locator.aspx?name=Common.History.Case&amp;RenderAs=feed&amp;ContentType=xml URL 放入您的浏览器选项卡并检查您是否能够看到该页面或您是否被重定向到登录页面?
  • @vijayP:是的。当我将其放入浏览器 URL 时,我能够获取数据

标签: jquery ajax cross-browser


【解决方案1】:

问题是因为添加了对 jquery 库的多个引用。

错误是因为

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>

通过删除多个 jquery 引用并保留一个来修复。

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2019-04-04
    • 2013-03-14
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多