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?
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With