What is content-type and datatype in an AJAX request With Example?

20
604
content type ajax

What is an AJAX Request?

An AJAX request is an HTTP request that is made asynchronously. This means that the browser does not have to wait for the server to respond before it can continue processing the page. As a result, AJAX requests are faster and more efficient than traditional web requests.There are two key components of an AJAX request: content-type and dataType.

var data = {“name”:”Doug Weber”}
$.ajax({
dataType : “json”,
contentType: “application/json; charset=utf-8”,
data : JSON.stringify(data),
success : function(result) {
alert(result.success); // result is an object which is created from the returned JSON
},
});

Content-Type:-

Content-Type is a header that tells the server what kind of data is being sent in the request. There are many different types of data that can be sent in a request, so the Content-Type header is used to specify which type of data is being sent.

The most common types of data that are sent in requests are JSON and XML. JSON is a format for storing and transporting data that is based on JavaScript object notation. XML is a format for storing and transporting data that is based on Extensible Markup Language. Both JSON and XML can be used to send data in an AJAX request.

DataType:-

The dataType of an AJAX request specifies the format of the data that is expected to be returned from the server. The most common dataTypes are text, xml, json, and script. Text is the default dataType, and simply returns the server response as a string of text. XML returns the server response as an XML document, which can be processed using standard XML methods such as getElementsByTagName(). JSON returns the server

When making an AJAX request, the Content-Type header tells the server what kind of data is being sent in the request body. The DataType parameter tells the server what format to expect the data in the response body to be in. Most commonly, the same DataType is used for both the request and response bodies (e.g. text/html). However, it is also possible to specify different DataTypes for the request and response bodies (e.g. application/json for the request body and text/xml for the response body).

 

 

20 COMMENTS

  1. Закажите SEO продвижение сайта https://seo116.ru/ в Яндекс и Google под ключ в Москве и по всей России от экспертов. Увеличение трафика, рост клиентов, онлайн поддержка. Комплексное продвижение сайтов с гарантией.

LEAVE A REPLY

Please enter your comment!
Please enter your name here