3

UPDATE 2:

I did not mention that I do not get the error below in Chromium, but I do get it in IE8.

UPDATE 1:

Response Headers:
HTTP/1.1 200 OK
Date: Fri, 20 May 2011 15:40:23 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 85

ORIGINAL QUESTION:

I have a serverside page which generates json data, which works perfectly well when accessed by jquery. However, when i navigate to the webpage which actually generates the json data, I get the following clientside error message in the browser:

The XML page cannot be displayed 
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later. 


--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'http://companyname.com/json/data.aspx?id=7

[{"key":7,"value":"Default"}]

Why does that happen and how can I stop the error message from appearing?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • Which response headers does the web page send? – Andomar May 20 '11 at 10:19
  • 2
    That's the official content type, so I assume your browser can't display JSON. I think that's OK, why would your users want to open JSON in a browser? – Andomar May 20 '11 at 16:00

3 Answers3

4

Same problem here, context.Response.ContentType="text/plain"; made my day...

Rodrigo
  • 4,365
  • 3
  • 31
  • 49
  • This resolved my issue. Answer "http://stackoverflow.com/a/6070509/24975" did not. – Maxime Rouiller Dec 16 '11 at 16:20
  • The semantically correct type is still application/json and it is probably a IE limitation that produces the error if it works in Chrome. See [this post](http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-do) referenced above on IE's JSON Support. – jm_toball Dec 19 '11 at 14:20
  • `text/plain` worked for me. Be sure to set `dataType: 'json'` in your call to `$.ajax()`. According to jQuery docs: "If none (dataType) is specified, jQuery will try to infer it based on the MIME type of the response ". I take that to mean if you use `text/plain`, jQuery is just going pass a string to your success function. – Walter Stabosz Mar 21 '13 at 20:19
  • This also fixed my issue. I was using jquery-file-upload so the contentType was being set for me (multipart) and I needed to return JSON from a handler. It worked in Chrome but not IE8, gave me `Invalid at the top level of the document` error. Switching to `text/plain` solved it even though it's not "correct." – kamranicus Jun 04 '13 at 17:30
1

The error appears, because the JSON data is probably served as type text/xml and your browser tries to interpret the output as XML. If you served it as application/json that would probably not happen. Why would you want to directly access the page though?

jm_toball
  • 1,207
  • 8
  • 10
  • 1
    I'm sending the json data back as `Response.ContentType = "application/json"`. – oshirowanen May 20 '11 at 15:39
  • Please check out update 1 above for proof of this from the response headers. – oshirowanen May 20 '11 at 15:50
  • 3
    See this related question about IE8 and JSON data: http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-dow – jm_toball May 20 '11 at 16:17
  • Seems like IE directs `application/json` to the XML viewer which itself tries to transform XML with an XML stylesheet - and fails miserably on the "invalid XML". – oleschri May 24 '11 at 12:07
0

Two tips here: 1) Install HttpFox Addon in Firefox which allows you to see exactly what is being sent/received 2) Install JSON View in Firefox which allows you to view JSON response in the browser.

BonyT
  • 10,750
  • 5
  • 31
  • 52
  • Please see update 1 in the original question. I don't have firefox, but according to chromium the response header show in update 1 is what it is returning. – oshirowanen May 20 '11 at 15:43