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?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • "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 – zerkms Feb 03 '13 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! – adeneo Feb 03 '13 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. – Kippie Feb 03 '13 at 22:41
  • 1
    Here (http://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'. – Andrew Briggs Feb 03 '13 at 22:43

1 Answers1

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/

charlietfl
  • 170,828
  • 13
  • 121
  • 150
hAmpzter
  • 317
  • 2
  • 5