7

I want to perform an ajax call with parameters. I want to send the paramters as json or as text if this is possible.

The result is returned as html content type.

So this is what I tried

  var data2 = {
      'some-id': 5
  };

  $.ajax({
        type: "POST",
        url: /* some url */,
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(data){      
            //some logic
        }
   }).fail(function() {
            //some error logic
   });

The problem is, that the ajax fails with the message "undefined" because it expects html as response, however my action returns html.

How can I make this to work with html response?

4
  • "the ajax fails with the message "undefined"" --- what outputs that message? PS: you don't need to stringify js object before sending, it's weird Commented Feb 3, 2013 at 22:40
  • $.ajax accepts an object directly in the data parameter, it's converted internally to a string, no need to do it manually? If you're receiving HTML, that would be a serverside issue! Commented Feb 3, 2013 at 22:40
  • Your question isn't completely clear to me. Do you need to tell the server to send only html as an answer? If so, use the "accepts" option in your ajax call. Commented Feb 3, 2013 at 22:41
  • 1
    Here (api.jquery.com/jQuery.ajax) it says that the setting, 'dataType', is "The type of data that you're expecting back from the server". So wouldn't it be: dataType: 'html'. Commented Feb 3, 2013 at 22:43

1 Answer 1

15

Just set the "dataType" to "html".

The parameter "dataType" is what type the jQuery ajax call expects in return.

More info: http://api.jquery.com/jQuery.ajax/

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

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.