$ .getJSON返回undefined成功回调在IE9及以下 [英] $.getJSON returning undefined to success callback in IE9 and below

查看:585
本文介绍了$ .getJSON返回undefined成功回调在IE9及以下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经遇到了一个奇怪的问题,一个$ .getJSON调用它似乎只影响到老版本的IE。 code是如下:

We've encountered an odd problem with a $.getJSON call which only seems to affect older versions of IE. Code is as follows:

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

$.getJSON(url, success);

在Chrome浏览器,火狐,IE10,这code的作品就好 - 的getJSON击中URL(这是有效的,和不是跨域),返回200 OK并返回数据,它是通过对成功起到你所期望的。

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.

但在IE9及以下的,成功的回调函数,而是通过传递的响应参数是不确定的。通过捕获在IE开发工具的网络流量,我可以看到电话打到URL,返回200 OK和响应主体返回有效JSON。那么,为什么是这样走出来为未定义当它击中了成功的回调?

However under IE9 and below, the success callback is invoked but the response parameter passed through is undefined. 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. So why is this coming out as undefined when it hits the success callback?

我已经使用$就调用,而不是用适当的参数尝试过了,我看到相同的行为。低于code:

I've tried using the $.ajax call instead with appropriate parameters, and I see the same behaviour. Code below:

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

我们正在使用jQuery 1.7.2(这样我们就能把该页面下的jQuery的新版本打破了图书馆之一,因此旧版本)。

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.10.1,不能解决问题

Just tried updating the page to use jQuery 1.10.1, doesn't fix the issue.

编辑2:我已经证实,正在返回JSON数据通过jsonlint.com是有效的,这样是不是问题或者

EDIT 2: I've confirmed that the JSON data being returned is valid via jsonlint.com so that isn't the issue either.

推荐答案

原来这不是一个jQuery的任何问题 - 它是与服务器返回的内容类型标头。无论出于何种原因(目前还不清楚究竟是什么改变来使这个问题)由服务器返回的媒体类型(尽管是普通的旧应用程序/ JSON)是不是在旧版本的IE跨preTED正确的jQuery 。我做了更改到服务器端code返回一个特定供应商的JSON内容类型和code现在工作了。

Turns out this wasn't a jQuery issue whatsoever - it was something to do with the content-type headers being returned by the server. 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. I made a change to the server side code to return a vendor specific JSON content type and the code now works again.

编辑:它发生,我认为修改服务器端之一是从0.17版本到的南希我们使用请求提供服务的框架。我猜这可能会影响到一些微妙的方式头,虽然我还没有追查到底如何。

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. I'm guessing this may have affected the headers in some subtle way, though I still haven't tracked down exactly how.

这篇关于$ .getJSON返回undefined成功回调在IE9及以下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆