Example analysis of data parameter type of Ajax() in JQuery_jquery
This article analyzes the data parameter type of Ajax() in JQuery through examples. Share it with everyone for your reference, the details are as follows:
The previous analysis briefly introduced "Two ways of passing data parameters in ajax". Now that we have a preliminary understanding of the ways of passing data parameters in ajax, let's further analyze the types of data parameters in ajax.
Suppose there is such a form now, which is used to add elements.
<form id='addForm' action='UserAdd.action' type='post'> <label for='uname'>用户名</label>:<input type='text' name='uname' id='uname'><br> <label for='mobileIpt'>手机号:</label><input type='text' name='mobileIpt' id='mobileIpt'><br> <label for='birthday'>生日:</label><input type='text' name='birthday'><br> <input type='button' value='提交' onclick='addUser()'> </form>
We don’t want to use form submit to add this element, we want to use ajax to submit.
This is how we implemented it before:
function addUser(){ var user = { uname:$("#uname").val(), mobileIpt:$("#mobileIpt").val(), birthday:$("#birthday").val() }; $.ajax({ url:'UserAdd.action', data:user, type:'post', dataType:'text', success:function(msg){ if(msg=='1'){ console.log('添加成功'); }else{ console.log('添加失败') } } }) }
There is nothing wrong with this, but it is too troublesome to get the values of form elements.... There are only three items here, and it will be useless if there are many items....
Until one day, I discovered jquery’s serializeArray method
Serialized table elements (similar to '.serialize()' method) return JSON data structure data.
Note that this method returns a JSON object rather than a JSON string. You need to use a plug-in or third-party library for stringification operations.
The returned JSON object is composed of an object array, where each object contains one or two name-value pairs - name parameter and value parameter (if value is not empty).
Let’s give it a try
$('#addForm').serializeArray(); //返回数据结构,是json数组,每个对像分别name和value为key,代表这个表单元素的name和value [ {"name":"uname","value":""}, {"name":"mobileIpt","value":""}, {"name":"birthday","value":""} ]
This seems to be useless
Let’s use the JQuery.param() method to handle it:
var arr = $('#addForm').serializeArray(); $.param(arr); "uname=alice&mobileIpt=110&birthday=1983-05-12"
Hey, now it meets our needs. Although it is not of json type, it can at least be uploaded as data.
Here we can directly fill in the json array in the ajax data, and call $.param() inside jquery to process it.
Let’s take a look at the description of the jquery.param() method:
Return value: StringjQuery.param(obj,[traditional])
Serialize the form element array or object.
Parameters:
obj,[traditional]
Arrays or jQuery objects will be serialized according to name/value pairs, and ordinary objects will be serialized according to key/value pairs.
traditional: Whether to use traditional shallow serialization.
demo:
$.param({uanme:'vic',mobileIpt:'110',birthday:'2013-11-11'}); "uanme=vic&mobileIpt=110&birthday=2013-11-11"
Looking at the description, it seems that it has nothing to do with us. Let’s change it to a json array
$.param([{uanme:'vic'},{mobileIpt:'110'},{birthday:'2013-11-11'}]); "undefined=&undefined=&undefined="
Is this conversion unsuccessful? Why can the data in our form be successfully converted into url parameters? Let’s take a look at the jquery source code
//在ajax()方法中,对json类型的数据进行了$.param()处理 if ( s.data && s.processData && typeof s.data !== "string" ) { s.data = jQuery.param( s.data, s.traditional ); } //param方法中 if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) { // Serialize the form elements jQuery.each( a, function() { add( this.name, this.value ); }); } else { // If traditional, encode the "old" way (the way 1.3.2 or older // did it), otherwise encode params recursively. for ( prefix in a ) { buildParams( prefix, a[ prefix ], traditional, add ); } }
Now you understand, if it is json data, then loop one by one and only take their name attribute and value attribute to splice the string.
If it is a normal object, loop the properties of the object and then concatenate the strings.
Summary:
So, what this article is going to say is that in the ajax function of jquery, 3 types of data can be passed in
1. Text: "uname=alice&mobileIpt=110&birthday=1983-05-12"
2.json object: {uanme:'vic',mobileIpt:'110',birthday:'2013-11-11'}
3.json array:
[ {"name":"uname","value":"alice"}, {"name":"mobileIpt","value":"110"}, {"name":"birthday","value":"2012-11-11"} ]
So, we can get the form and submit it with one click, which is very convenient.
Supplement:
In fact, to extract form data, you only need to use the serialize() method to directly obtain "uname=alice&mobileIpt=110&birthday=1983-05-12".
I hope this article will be helpful to everyone in jQuery programming.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
