0

I have some jQuery which is just fine:

$.ajax({
    url: actionUrl,
    type: "POST",
    data: data,
    dataType: "json",
    success: function (data) {
        alert(data)
    }
});

However, instead of handling the data, the browser tries to open the file. Why?

I'm guessing there is anything wrong with the MIME type of the response, but it works pretty fine other places in the code.

Helge
  • 823
  • 5
  • 13
  • 28
  • 1
    The response MIME type should be `application/json`. References: [StackOverflow question](http://stackoverflow.com/questions/477816/the-right-json-content-type), [RFC 4627 section 6](http://tools.ietf.org/html/rfc4627), and [Wikipedia](http://en.wikipedia.org/wiki/JSON). – Mike DeSimone Aug 31 '11 at 11:52
  • is it happening in all the browsers? try setting the `contentType:"application/json charset=utf-8",` – Rafay Aug 31 '11 at 11:52

2 Answers2

0

could you possibly provide the link in actionUrl? Or is that private?

The code you've given is totally correct, so the problem must be with the server returning the wrong header for the return stream. The response header should be text/json. If it's in the format: "application/.." that would be incorrect.

You can view the the return headers for that url via tools like fiddler, poster (firefox addon), firebug (firefox addon), chrome developer view, etc..

Can you check and report back?

Bram Vandenbussche
  • 1,401
  • 11
  • 22
  • 1
    It should be `application/json`. See my comment on the question. Per RFC 4627: "JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON is written in UTF-8, JSON is 8bit compatible. When JSON is written in UTF-16 or UTF-32, the binary content-transfer-encoding must be used." Since it is not *always* `text`, it got bumped to `application`. – Mike DeSimone Aug 31 '11 at 11:54
  • True, I figured he would just ust the plain utf-8 encoding. Thanks for the elaborate explaination. – Bram Vandenbussche Aug 31 '11 at 11:56
0

try

  $.ajax({
        url: actionUrl,
        type: "POST",
        data: data,
        contentType:'application/json charset=utf-8',
        dataType: "json",
        success: function (data) {
            alert(data)
        }
    });
Rafay
  • 30,950
  • 5
  • 68
  • 101