jquery ajax send and receive html on server code example
Example 1: jquery ajax post example
var formData = {name:"John", surname:"Doe", age:"31"}; //Array
$.ajax({
url : "https://example.com/rest/getData", // Url of backend (can be python, php, etc..)
type: "POST", // data type (can be get, post, put, delete)
data : formData, // data in json format
async : false, // enable or disable async (optional, but suggested as false if you need to populate data afterwards)
success: function(response, textStatus, jqXHR) {
console.log(response);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
Example 2: js ajax receive html
$.ajax({
type: 'POST',
url: "<Your URL>",
contentType: 'application/json; charset=utf-8'
// Set your dataType to either 'html' or 'text'.
// Keep in mind: dataType is for receiving,
// contentType is for sending
dataType: 'html',
data: { example: 1, id: "0x100"},
// Note: the data above is used in sending,
// data below is a variables that stores received data
success: function (data){
// Suppose you have an html element, where you want to append
// the response:
$('#<Your html element id>').html(data);
// .html(data) overwrites existing data
// Use .append(data) to add response without overwriting!
}
});