Home  >  Article  >  Web Front-end  >  jQuery's method of passing json format data to the background_jquery

jQuery's method of passing json format data to the background_jquery

WBOY
WBOYOriginal
2016-05-16 16:14:071151browse

The example in this article describes how jQuery transmits json format data to the background. Share it with everyone for your reference. The specific analysis is as follows:

Front and backend data interaction generally uses json format, and the backend can directly convert json correspondence into entity objects. To facilitate future operations. When jQuery transmits data to the background, we will find that it will automatically convert it into a query string and cannot actually pass in a json. Moreover, when using jquery to serialize the form, the returned format is an array, which requires further conversion. In fact, as long as we configure something in the ajax method, it can be completed. The code is as follows:

<form id="ff"> 
  <input type="text" name="test1"/> 
  <input type="text" name="test2"/> 
  <input type="text" name="test3"/> 
  <input type="text" name="test4"/> 
  <input type="button" id="save" value="save"/> 
</form> 

$("#save").on("click", function () { 
 var params = $("#ff").serializeArray(); 
 var j = {}; 
 for (var item in params) { 
   j[params[item].name] = params[item].value; 
 } 

 $.ajax({ 
   url:'index.html', 
   data:JSON.stringify(j), 
   type:'post', 
   dataType:'json', 
   headers:{ 
 Accept:"application/json", 
 "Content-Type":"application/json" 
   }, 
   processData:false, 
   cache:false 
 }).done(function (data) { 
 }); 

}); 

If you see the display format as shown in the picture in chrome, it means that the json format passed to the background is

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn