开发者

IE bugs when writing JSON via an ASPX response

开发者 https://www.devze.com 2023-01-04 02:01 出处:网络
I have an ASPX page that I am using to write JSON. It works great in Firefox and Chrome, but when I try and use it in IE 8 it gives me an \"The XML page cannot be displayed\" error instead of allowing

I have an ASPX page that I am using to write JSON. It works great in Firefox and Chrome, but when I try and use it in IE 8 it gives me an "The XML page cannot be displayed" error instead of allowing jQuery to load the JSON being written by the response.

Any ideas?

Here is what my code looks like:

protected override void OnLoad(EventArgs e) {
 Response.Clear();
 Response.ClearHeaders();
 Response.ContentType = "application/json";
 Re开发者_如何学编程sponse.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.Write(string.Format("[ {{ \"Foo\": \"{0}\", \"bar\": \"{1}\" }} ]", "Foo Content", "Bar Content"));
 Response.End();
}


I ended up "fixing" the problem by not specifying a Content Type. Not sure why it didn't work with the context type as noted above. I've used it for other things without a problem.


Can you post the jQuery you are using? The problem's likely to be there, as there are some differences in the way IE and Firefox handle Javascript and AJAX requests.

From that error, it sounds like IE might be trying to load the JSON as a webpage, instead of making an AJAX request.


what if you get rid of the [ ]'s in the json string? that doesn't look necessary

also, you can use JSON serializer that is built right into .net, and it will definitely be ie8 compatible.

System.Web.Script.Serialization.JavaScriptSerializer

Link

0

精彩评论

暂无评论...
验证码 换一张
取 消