javascript ie8/9 中的 jquery ajax 调用不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19551681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:57:17  来源:igfitidea点击:

jquery ajax call in ie8/9 not working

javascriptjqueryhtmlajaxinternet-explorer

提问by AGarrett

I feel like I should be able to find this anwser but have had zero success after hours of research. I have this pagewith a simple jquery ajax calls to a API service. It works fine in Chrome, Safari, and Firefox, and even IE 10. But IE 9 and 8 seem to fail.

我觉得我应该能够找到这个答案,但经过数小时的研究,成功率为零。我有这个页面,其中包含对 API 服务的简单 jquery ajax 调用。它在 Chrome、Safari 和 Firefox,甚至 IE 10 中运行良好。但 IE 9 和 8 似乎失败了。

Here's the code:

这是代码:

 $.ajax({
                    type: "GET",
                    url: "http://api.domain.org/api/campus?filtertype=name&filter="+ escape($('#campus').val()),
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {
                        $('#results').children().remove();
                        var arrayd = (typeof result) == 'string' ? eval('(' + result + ')') : result;
                        if (arrayd != null) {
                            for (var i = 0; i < arrayd.length; i++) {
                                $('#results').append('<li>' + arrayd[i].SchoolName + '</li>');
                            }
                        }
                    },
                    error: function (request, status, errorThrown) {
                        alert("There was an issue with the request");
                    } // When Service call fails
                });

The biggest issue is that the alert files, but I can't see any traffic in the IE Debugging tools which doesn't help me see if I even hit the server.

最大的问题是警报文件,但我在 IE 调试工具中看不到任何流量,这无法帮助我查看是否访问了服务器。

I've tried a number of things:

我尝试了很多事情:

  1. Setting $.ajax setting per http://mvcdiary.com/2013/01/16/jquery-ajax-request-not-working-in-ie9/
  2. Setting '$.support.cors = true;' before the ajax call
  3. Using 'jquery.iecors.js'
  1. 根据http://mvcdiary.com/2013/01/16/jquery-ajax-request-not-working-in-ie9/设置 $.ajax 设置
  2. 设置 '$.support.cors = true;' 在 ajax 调用之前
  3. 使用'jquery.ieco​​rs.js'

Update

更新

After changing the dataType to 'jsonp' I started seeing the traffic in IE 8/9 with successful calls. However, I'm not getting any browser to call the success method now.

将 dataType 更改为 'jsonp' 后,我开始在 IE 8/9 中看到成功调用的流量。但是,我现在没有让任何浏览器调用成功方法。

 $.ajax({
                    type: "GET",
                    url: "http://api.athletesinaction.org/api/campus?filtertype=name&filter="+ escape($('#campus').val()),
                    dataType: "jsonp",
                    async: false,
                    contentType: "application/javascript",
                    crossDomain: true,
                    jsonpCallback: 'myTest',
                    success: myTest,
                    error: function (request, status, errorThrown) {
                        alert(errorThrown);
                    } // When Service call fails
                });


        function myTest (argument) {
            alert("YEAH");
            $('#results').children().remove();
            var arrayd = (typeof result) == 'string' ? eval('(' + result + ')') : result;
            if (arrayd != null) {
                for (var i = 0; i < arrayd.length; i++) {
                    $('#results').append('<li>' + arrayd[i].SchoolName + '</li>');
                }
            }
        }

采纳答案by earl3s

Try changing the dataTypeto jsonp.

尝试将 更改dataTypejsonp

Though the service will need to support this type of request.

虽然服务需要支持这种类型的请求。

回答by Igor Benikov

Yes, it's not works in IE8.

是的,它不适用于 IE8。

If you need to support IE8, take JSONP or postMessage, or combination of both.

如果你需要支持 IE8,采用 JSONP 或 postMessage,或者两者的结合。