jQuery的$。员额第二个参数。 - JSON或查询字符串? [英] jquery $.post second argument. - json or query string?

查看:133
本文介绍了jQuery的$。员额第二个参数。 - JSON或查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.post('somescript.php', { data: $('#myInputField').val() },
    function(replyData) {

1) 这是$。员额方法的第二个参数 - ?以JSON

1) Is the second argument of this $.post method - in json?

2) 这是$。员额方法的第二个参数的查询字符串?

2) Is the second argument of this $.post method a query string?

在此先感谢, MEM

Thanks in advance, MEM

注意:如果这个问题没有意义,请知道的为什么(也没有意义),也可以寻求帮助,被视为一个有效的答案为好。

Note: If this question doesn't make sense, please, knowing why (it doesn't make sense) may also help and, be taken as an valid answer as well.

推荐答案

在一般的两种方式在实践中的非常接近的。函数 $。交 $。阿贾克斯将连接code数据公布的在同一方法。如果您想张贴JSON数据,你应该另外连接code中的数据的与一些JSON EN codeR。请参阅<一href="http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086">http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086作为一个例子( $。交 $的缩写形式。AJAX ,所以所有这些与所描述 $。阿贾克斯,正确也为 $。交

In general both ways are in practice very close. The function $.post or $.ajax will encode the data posted in the same way. If you want to post JSON data you should additionally encode the data value with some JSON encoder. See http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086 as an example ($.post is a short form of $.ajax, so all which described with $.ajax and correct also for $.post)

$.post('somescript.php', { data: JSON.stringify($('#myInputField').val()) }, ...);

在code以上我用 JSON.stringify 从的 http://www.json.org/js.html

In the code above I use JSON.stringify from http://www.json.org/js.html.

更新时间::您的问题在评论我希望以后我更明白,你想知道的。所以 jQuery.post 不要让数据为您的任何JSON编码作为输入参数( jQuery.post )。因此它以同样的方式发送数据总是精确。您可以到 $。后添加额外的JSON参数(最后一个的dataType 参数)调用,但这会不改变数据将如何连接codeD。

UPDATED: After your questions in the comment I hope I understand more which you want to know. So jQuery.post don't makes any JSON encoding of data for you for and input parameters (the second parameter of jQuery.post). So it send the data always exactly in the same way. You can add additional "json" parameter (the last dataType parameter) to the $.post call, but this will not change how the data will be encoded.

这个问题:我应该发送JSON数据到服务器或没有?独立于 $。交,你应该对自己依赖于现有项目中的需求的问题答案。有时它是你的解决方案的体系结构的问题。有时,你必须选择一个特殊的办法。

The question "should I send JSON data to the server or not?" is independent on $.post and you should answer on the question yourself depend on the requirement existing in your project. Sometime it is the question of the architecture of your solution. Sometime you have to choose one special way.

在微软的情况下ASMX Web服务的例子也存在一些重要的限制。比如你想从Web服务提供JSON数据,以便能够方便工作与JavaScript中的数据。所以,你想拥有在服务器端具有一定的输入参数和返回JSON作为输出的方法。如果ASMX Web服务您必须发送的所有输入参数到Web服务方法,JSON EN codeD数据,以便能够从Web服务返回JSON数据,但ASMX Web服务去code / EN code为你的数据,你不需要手动连接code /德code服务器端的JSON。

In case of Microsoft ASMX Web Service for example there exist some important restriction. For example you want to deliver JSON data from the web service to be able to work easy with the data in JavaScript. So you want to have a method on the server side which have some input parameters and returns JSON as output. In case ASMX Web Service you must sent all input parameter to the web service method as JSON encoded data to be able to return JSON data from the web service, but ASMX Web Service decode/encode the data for you and you don't need manually encode/decode JSON on the server side.

这篇关于jQuery的$。员额第二个参数。 - JSON或查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆