简体   繁体   English

$ .getJSON在IE9及以下版本中将undefined返回到成功回调

[英]$.getJSON returning undefined to success callback in IE9 and below

We've encountered an odd problem with a $.getJSON call which only seems to affect older versions of IE. 我们遇到了一个奇怪的问题,$ .getJSON调用似乎只影响旧版本的IE。 Code is as follows: 代码如下:

var success = function (response) {
     // do stuff
}

$.getJSON(url, success);

Under Chrome, Firefox, IE10, this code works just fine - getJSON hits the URL (which is valid, and is not cross domain ), returns 200 OK and returns the data, which is passed through to the success function as you'd expect. 在Chrome,Firefox,IE10下,这段代码工作得很好 - getJSON点击URL(有效, 不是跨域 ),返回200 OK并返回数据,然后按照您的预期传递给成功函数。

However under IE9 and below, the success callback is invoked but the response parameter passed through is undefined. 但是在IE9及更低版本下,将调用成功回调,但未定义传递的响应参数。 By capturing the network traffic in IE Dev Tools I can see the call hitting the URL, returning 200 OK and returning valid JSON in the response body. 通过在IE开发工具中捕获网络流量,我可以看到呼叫到达URL,返回200 OK并在响应正文中返回有效的JSON。 So why is this coming out as undefined when it hits the success callback? 那么,当它遇到成功回调时,为什么会出现这种情况?

I've tried using the $.ajax call instead with appropriate parameters, and I see the same behaviour. 我尝试使用$ .ajax调用而不是使用适当的参数,我看到了相同的行为。 Code below: 代码如下:

$.ajax({
    dataType: "json",
    url: url,
    success: success
};

We're using jQuery 1.7.2 (one of the libraries we've got on the page is broken under the newer version of jQuery, hence the old version). 我们正在使用jQuery 1.7.2(我们在页面上获得的一个库在较新版本的jQuery下被破坏,因此是旧版本)。

EDIT: Just tried updating the page to use jQuery 1.10.1, doesn't fix the issue. 编辑:刚尝试更新页面使用jQuery 1.10.1,并没有解决问题。

EDIT 2: I've confirmed that the JSON data being returned is valid via jsonlint.com so that isn't the issue either. 编辑2:我已经确认返回的JSON数据是通过jsonlint.com有效的,所以这也不是问题。

If it is caching the result then set the cache to false before the request: 如果它正在缓存结果,则在请求之前将缓存设置为false:

$.ajaxSetup({ cache: false });

If it is caching the result then use ajax instead of getJSON: 如果它正在缓存结果,那么使用ajax而不是getJSON:

$.ajax({
    url: 'data.json',
    cache: false,
    dataType: 'json',
    success: function(data) {
        console.log('success', data);
    },
    error: function (request, status, error) {
        console.log('error', error);
    }
});

If the mime type is wrong then specify the dataType: 如果mime类型错误,则指定dataType:

$.getJSON('data.json', 'json', function(data) {
    console.log('getJSON', data);
});

If the mime type is wrong then specify the dataType on the server: 如果mime类型错误,则在服务器上指定dataType:

header('Content-type: application/json');

If it's a variable conflict then rename your callback variable: 如果它是变量冲突,则重命名您的回调变量:

var success = function (newname) {
    console.log('success', newname);
}
$.getJSON(url, success);

If it's cross origin request then you can force jQuery to use it with: 如果它是跨源请求,那么您可以强制jQuery使用它:

$.support.cors = true;

Turns out this wasn't a jQuery issue whatsoever - it was something to do with the content-type headers being returned by the server. 事实证明这不是一个jQuery问题 - 它与服务器返回的内容类型标题有关。 For whatever reason (still not clear exactly what changed to cause the issue) the media type being returned by the server (despite being plain old application/json) wasn't being interpreted properly by jQuery under the older versions of IE. 无论出于何种原因(仍然不清楚究竟是什么原因导致问题)服务器返回的媒体类型(尽管是普通的旧应用程序/ json)在旧版本的IE下没有被jQuery正确解释。 I made a change to the server side code to return a vendor specific JSON content type and the code now works again. 我对服务器端代码进行了更改,以返回特定于供应商的JSON内容类型,现在代码再次运行。

EDIT: It occurs to me that one of the changes server side was an upgrade from version 0.17 to version 0.20 of the Nancy framework we use to serve the requests. 编辑:我发现其中一个更改服务器端是从我们用来提供请求的Nancy框架的版本0.17升级到版本0.20。 I'm guessing this may have affected the headers in some subtle way, though I still haven't tracked down exactly how. 我猜这可能会以某种微妙的方式影响标题,但我仍然没有找到确切的方法。

A little late to the game but I'll share my solution for anyone stumbling upon this thread: 游戏有点晚,但我会为任何绊倒这个线程的人分享我的解决方案:

I had the same issue, but the cause turned out to be a little different. 我有同样的问题,但原因结果有点不同。 I was passing tags to the getJSON url that contained special characters. 我将标签传递给包含特殊字符的getJSON网址。 Now Chrome silently converts these characters for me, but IE does not, which caused the server to return empty data. 现在Chrome默默地为我转换这些字符,但IE没有,这导致服务器返回空数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM