0

I am posting some JSON data to my action method like so:

$.ajax({
        url: this.options.url,
        type: 'POST',
        dataType: 'json',
        data: values,
        success: function (html, status, response) {
            // do whatever
        },
        error: function (error) {
            // do whatever
        }
    })

I am succesfully hitting the server, with the correct values present, no errors are being thrown but yet the error event is hit up completion, not the success event. On inspecting the response object in the error event I can see that I am getting a 200 'OK' back. I can also see that response text is what I would expect it to be.

I assume this is because I am posting JSON but returning text? Is it possible to have a different dataType for each direction?

1
  • Are you returning valid JSON ? Commented Apr 23, 2012 at 13:56

5 Answers 5

5

The dataType property is used to signify the type of data you're expecting in response to the call, not the type of data you're sending, so your call is expecting a JSON response, but is getting plain text instead.

Sign up to request clarification or add additional context in comments.

Comments

3

The datatype you are specifying is for the response type. Simply change that to text and it will work.

The error is raised, because jQuery tries to parse your text response as JSON and thus runs into problems.

$.ajax({
        url: this.options.url,
        type: 'POST',
        dataType: 'text',
        data: values,
        success: function (html, status, response) {
            // do whatever
        },
        error: function (error) {
            // do whatever
        }
})

Comments

1

Short answer is, of course yes.

Via http://api.jquery.com/jQuery.ajax/ dataType refers to

The type of data that you're expecting back from the server.

NOT to what you are sending. Meanwhile, if you are sending JSON you are in fact just sending text -- text that happens to contain JSON content.

So, possibly you just need to change dataType to say you are returning text, and you might be golden.

Comments

0

Check the dataType attribute. jQuery is expecting a JSON response. From the doc:

"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

Comments

0

Your are expecting data as a text format.

$.ajax({
        url: this.options.url,
        type: 'POST',
        dataType: 'text', // instead of json
        data: values,
        success: function (html, status, response) {
            // do whatever
        },
        error: function (error) {
            // do whatever
        }
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.