开发者

What is the purpose of data option in jQuery.ajax() function?

开发者 https://www.devze.com 2023-02-07 02:50 出处:网络
$.ajax({ type:\"post\", data:开发者_JS百科\"name=\"+name+\"&article=\"+article\", what is the two name mean in the part of data?Read jQuery.ajax()
$.ajax({
type:"post",
data:开发者_JS百科"name="+name+"&article="+article",

what is the two name mean in the part of data?


Read jQuery.ajax()

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting

For example: You are posting name and location to a PHP script to store in database like this.

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

Now In some.php file you can access POST values like this:

$_POST['name']; // John
$_POST['location']; // Boston


its the data that you are sending to the server for processing. so in your example you are sending the server:

field "name"
value "whatever is in the name variable"

so now the server can look up the the name field, use its value to do whatever it is you are asking the server to do.


$.ajax({
   type: "POST",
   url: "some.php",
   data: {
       name: "John",
       location: "Boston"
   },
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});

if it is not helpful please check the fallowing link

0

精彩评论

暂无评论...
验证码 换一张
取 消