简体   繁体   English

数据的用法和含义:jQuery ajax + JSON中的{get_param:'value'}

[英]Use and meaning of data: { get_param: 'value' } in jQuery ajax + JSON

I am parsing generated json with jquery $.ajax but there is one option that I dont understand. 我用jquery $ .ajax解析生成的json,但有一个我不明白的选项。 I saw it in some examples and tried to look for at jquery.com but still not sure about it: 我在一些例子中看到它并试图在jquery.com上寻找但仍不确定:

this option is: 这个选项是:

data: { get_param: 'value' }

which is used like this: 这是这样使用的:

$.ajax({ 
                type: 'GET', 
                url: 'http://example/functions.php', 
                data: { get_param: 'value' }, //why we shell use that in that case?
                success: function (data) { 
                                    var names = data
                    $('#cand').html(data);
                }
          });

I know that " data: " is what sent to the server but parsing JSON I thought i don't send but retrieve from server with GET type. 我知道“ data: ”是发送到服务器但是解析JSON我认为我不发送但是从具有GET类型的服务器检索。 And the next part " get_param: 'value' " does not make sense to me in that case either, could anyone please explain when and what for and in what cases it shell be used? 在这种情况下,下一部分“ get_param:'value' ”对我来说也没有意义,是否有人可以解释何时以及在什么情况下使用shell?

thank you. 谢谢。

I know that "data" is what sent to the server 我知道“数据”是发送到服务器的内容

Yes. 是。 If data is an object, it gets serialized to an application/x-www-form-urlencoded string and then placed in the query string or request body as appropriate for the request type (GET/POST). 如果data是一个对象,它将被序列化为application/x-www-form-urlencoded字符串,然后根据请求类型(GET / POST)放置在查询字符串或请求正文中。

jQuery does all the escaping necessary for this. jQuery完成了所有必需的转义。

(It also, by default, collapses nested data structures (you don't have any in your example) into PHP-style by adding [] to key names). (默认情况下,通过将[]添加到键名称,将嵌套数据结构(在您的示例中没有任何内容)折叠为PHP样式)。

but parsing JSON 但解析JSON

JSON is not involved (unless the server responds with some). 不涉及JSON(除非服务器响应一些)。

when and what for and in what cases it shell be used 什么时候以及在什么情况下使用shell

Whenever you want to pass data to the server rather than requesting a static URI. 每当您想要将数据传递到服务器而不是请求静态URI时。

You don't send JSON (usually), you send simple GET or POST HTTP parameters. 您不发送JSON(通常),您发送简单的GET或POST HTTP参数。 They are given to the ajax method in an object literal usually, but you could have used the string "getparam=value" , too. 它们通常用于对象文字中的ajax方法,但你也可以使用字符串"getparam=value" If you provide an object, jQuery will do the parameter-serialisation and URL-encoding for you - they're sent as x-www-form-urlencoded . 如果你提供一个对象,jQuery会为你做参数序列化和URL编码 - 它们被发送为x-www-form-urlencoded

To cite from the docs : 文档中引用:

data ( Object , String ) 数据ObjectString

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是一个数组,jQuery会根据traditional设置的值使用相同的键序列化多个值。

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

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