【问题标题】:Problem - Ajax call on IE only works when another page with the same Ajax call is open in the browser问题 - IE 上的 Ajax 调用仅在浏览器中打开具有相同 Ajax 调用的另一个页面时才有效
【发布时间】:2019-05-01 16:41:54
【问题描述】:

我正在尝试从 SharePoint 内容编辑器对服务器端 API 进行 POST Ajax 调用。 API 返回 URL 和标题列表。然后将 URL 动态添加到 SharePoint 列表视图中。 这在 Chrome 中可以正常工作,但在 IE 上不行。 我收到XMLHttpRequest: Network Error 0x2ef3, could not complete the operation due to error 00002ef3

我在本地使用 Ajax 调用创建了一个测试 HTML,它运行良好。 奇怪的是,如果我在同一个浏览器上打开本地 HTML 文件,它在 IE 上的 SharePoint 页面上运行良好。 有人可以帮我解决吗?

这里是 AJAX 调用:

var response;
Var settings = {
“async”: true,
“crossDomain”:true,
“url”: url1,
“method”: “POST”,
“type”:”POST”,
“dataType”:”json”,
“Keep-Alive”:”timeout=0, max=1000”,
“Cache-Control”:”no-cache, no-store, must-revalidate”,
“Pragma”:”no-cache”,
“Expires”:”0”,
“headers”:{
“Content-Type”:”application/json; charset=utf-8”,
“api_key”:key1,
“Authorization”:”Bearer “ + tkn1
},
“complete”:function(text){
response=text.responseText;
},
“cache”:false,
“processData”: false,
“data”:data1()
};

function data1(){
return JSON.stringify(data2);
}

jQuery.support.cors=true;
$.ajax(settings).complete(function(){
var resObj=JSON.parse(response);
.....
});

【问题讨论】:

  • 似乎有几个可能的原因(例如参见hereherehere),您可以查看它们。
  • 感谢@ZhiLv-MSFT 但是这些解决方案没有奏效,但我设法解决了这个问题。将在下面回答!

标签: jquery json ajax internet-explorer sharepoint


【解决方案1】:

请记住,“如果我在同一浏览器上打开本地 HTML 文件,它在 IE 上的 SharePoint 页面上运行良好。” 通过跟踪和错误进行回溯,发现如果 method:POSTtype:POST 都在第二个 Ajax 调用中,并且第一个 Ajax 调用中只有 method: POST 没有 type: POST

我不确定它为什么以及如何工作,但确实如此。

我猜 IE 不知何故占用了缓存数据,而 cache:false 以及 Pragma: no-cacheExpires: 0 似乎永远不会工作。

这里是完整的解决方案:

var successSettings = {
    "async": true,
    "crossDomain": true,
    "url": url1,
    "method": "POST",
    "type": "POST",
    "dataType":"json",
    "Pragma": "no-cache",
    "Expires": "0",
    "headers": {
        "Content-Type": "application/json; charset=utf-8",
        "Authorization": tkn
        "api_key":key1
     },
     "success": function(text1){
         response = text.objlink;
     },
     "error": function(jqXHR, exception){
         var errorHandle = jqXHR + exception;
     },
     "cache": false,
     "processData": false,
     "data": dataSend()
};
var failSettings = {
    "async": true,
    "crossDomain": true,
    "url": url1,
    "method": "POST",
    "dataType":"json",
    "Pragma": "no-cache",
    "Expires": "0",
    "headers": {
        "Content-Type": "application/json; charset=utf-8",
        "Authorization": tkn
        "api_key":key1
     },
     "success": function(text1){
         response = text.objlink;
     },
     "error": function(jqXHR, exception){
        $.ajax(SuccessSettings).done(function () {
               var resObj = response;
               ....
        });
     },
     "cache": false,
     "processData": false,
     "data": dataSend()
};
jQuery.support.cors = true;
$.ajaxSetup({cache: false});
$.ajax(failSettings).done(function () {
     var resObj = response;
     ....
});

【讨论】:

    猜你喜欢
    • 2015-01-14
    • 1970-01-01
    • 2022-01-14
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多