简体   繁体   English

jQuery.ajax()函数中data选项的目的是什么?

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

$.ajax({
type:"post",
data:"name="+name+"&article="+article",

what is the two name mean in the part of data? 数据部分中的两个名字分别是什么意思?

Read jQuery.ajax() 阅读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. 它被附加到GET请求的URL上。 See processData option to prevent this automatic processing. 请参阅processData选项以防止这种自动处理。 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 如果value是一个Array,则jQuery根据传统设置的值使用相同的键序列化多个值

For example: You are posting name and location to a PHP script to store in database like this. 例如:您正在将名称和位置发布到PHP脚本中,以像这样存储在数据库中。

$.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: 现在,在some.php文件中,您可以访问POST值,如下所示:

$_POST['name']; // John
$_POST['location']; // Boston
$.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 如果没有帮助,请检查休假链接

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. 因此,现在服务器可以查找名称字段,使用其值执行您要服务器执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM