Home > Web Front-end > JS Tutorial > body text

Detailed explanation of data parameter type examples of jQuery.Ajax()

小云云
Release: 2017-12-31 16:17:23
Original
1736 people have browsed it

This article mainly introduces the detailed explanation of the data parameter type of jQuery.Ajax(). Friends in need can refer to it. I hope it can help everyone.

If there is such a form now, it is used to add elements.


<form id=&#39;addForm&#39; action=&#39;UserAdd.action&#39; type=&#39;post&#39;>
   <label for=&#39;uname&#39;>用户名</label>:<input type=&#39;text&#39; name=&#39;uname&#39; id=&#39;uname&#39;><br>
   <label for=&#39;mobileIpt&#39;>手机号:</label><input type=&#39;text&#39; name=&#39;mobileIpt&#39; id=&#39;mobileIpt&#39;><br>
   <label for=&#39;birthday&#39;>生日:</label><input type=&#39;text&#39; name=&#39;birthday&#39;><br>
   <input type=&#39;button&#39; value=&#39;提交&#39; onclick=&#39;addUser()&#39;>
</form>
Copy after login

We don’t want to use form submit to add this element, we want to use ajax to submit.

We used to implement it like this:


function addUser(){
    var user = {
      uname:$("#uname").val(),
      mobileIpt:$("#mobileIpt").val(),
      birthday:$("#birthday").val()
    };
    $.ajax({
      url:&#39;UserAdd.action&#39;,
      data:user,
      type:&#39;post&#39;,
      dataType:&#39;text&#39;,
      success:function(msg){
        if(msg==&#39;1&#39;){
          console.log(&#39;添加成功&#39;);
        }else{
          console.log(&#39;添加失败&#39;)
        }
      }
       
    })
  }
Copy after login

There is nothing wrong with this, but it is too troublesome to get the value of the form element.... Here is the only Three items, too many items are useless....

Until one day, I discovered jquery's serializeArray method

Serialize table elements (similar to the '.serialize()' method ) returns 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 try it


$(&#39;#addForm&#39;).serializeArray();
//返回数据结构,是json数组,每个对像分别name和value为key,代表这个表单元素的name和value
[
  {"name":"uname","value":""},
  {"name":"mobileIpt","value":""},  
  {"name":"birthday","value":""}
]
Copy after login

This doesn’t seem to be useful

We use the JQuery.param() method to handle it Here it is:


var arr = $(&#39;#addForm&#39;).serializeArray();
$.param(arr);
"uname=alice&mobileIpt=110&birthday=1983-05-12"
Copy after login

Hey, this meets our needs. Although it is not a 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])

Convert the form element array or object Serialization.

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:&#39;vic&#39;,mobileIpt:&#39;110&#39;,birthday:&#39;2013-11-11&#39;});
"uanme=vic&mobileIpt=110&birthday=2013-11-11"
Copy after login

Looking at the description, it seems that it has nothing to do with us. Let’s change it to a json array


$.param([{uanme:&#39;vic&#39;},{mobileIpt:&#39;110&#39;},{birthday:&#39;2013-11-11&#39;}]);
"undefined=&undefined=&undefined="
Copy after login

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 );
    }
  }
Copy after login

Now you understand, if it is json data, then loop one by one and only take their name attributes and value attributes to splice strings.

If it is a normal object, loop through 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"
Copy after login

2.json object:


{uanme:&#39;vic&#39;,mobileIpt:&#39;110&#39;,birthday:&#39;2013-11-11&#39;}
Copy after login

3.json array:


[
  {"name":"uname","value":"alice"},
  {"name":"mobileIpt","value":"110"},  
  {"name":"birthday","value":"2012-11-11"}
]
Copy after login

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".

Related recommendations:

javascript - php gets the data parameter, how to match it with the array

ajax method data in Jquery Summary of parameter usage_jquery

##jQuery form plug-in formDdata parameter verification form and submission after verification_jquery

The above is the detailed content of Detailed explanation of data parameter type examples of jQuery.Ajax(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template