Jereme
Jereme

Reputation: 1485

IE bugs when writing JSON via an ASPX response

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";
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.Write(string.Format("[ {{ \"Foo\": \"{0}\", \"bar\": \"{1}\" }} ]", "Foo Content", "Bar Content"));
 Response.End();
}

Upvotes: 1

Views: 728

Answers (3)

Sonic Soul
Sonic Soul

Reputation: 24909

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

Upvotes: 0

Jereme
Jereme

Reputation: 1485

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.

Upvotes: 1

minimalis
minimalis

Reputation: 1953

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.

Upvotes: 0

Related Questions