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.