Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax send json and return html

Tags:

jquery

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?

like image 370
UpCat Avatar asked Feb 03 '13 22:02

UpCat


People also ask

How to handle JSON response in jQuery AJAX?

In this tutorial, I showed how you can return the JSON response and handle it in jQuery AJAX. You can convert the PHP array in JSON format with json_encode() function and return as a response. Set dataType: 'JSON' when send AJAX request.

How to get AJAX response in JSON format?

Approach: To solve this problem, we will first consider a JSON file named “capitals. json” and try to get this JSON data as a response using AJAX. Then we will create an HTML file “capitals. html” which contains a table which we will use to populate the data we are getting in response.


1 Answers

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/

like image 146
hAmpzter Avatar answered Sep 28 '22 08:09

hAmpzter