jquery $.post 第二个参数。 - json 还是查询字符串?

发布于 2024-09-16 02:00:03 字数 353 浏览 1 评论 0原文

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

1) 这个 $.post 方法的第二个参数是 json 吗?

2) 这个 $.post 方法的第二个参数是查询字符串吗?

提前致谢, MEM

注意:如果这个问题没有意义,请了解原因(没有意义)也可能会有所帮助,并且也可以被视为有效答案。

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

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

OR

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-09-23 02:00:03

一般来说,这两种方式在实践中都非常接近。函数$.post$.ajax以相同的方式对发布的数据进行编码。如果您想发布 JSON 数据,您应该使用一些 JSON 编码器另外对数据进行编码。请参阅如何我是否构建一个 JSON 对象以发送到 AJAX WebService? 作为示例($.post$.ajax 的缩写形式,因此所有这些用 $.ajax 描述,并且 $.post 也正确)

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

在上面的代码中,我使用 http://www.json.org/js.html

更新:在评论中提出问题后,我希望我能了解更多您想知道的内容。所以 jQuery.post 不会为您进行任何 JSON 数据编码< /strong> 用于输入参数(jQuery.post 的第二个参数)。所以它总是以完全相同的方式发送数据。您可以向 $.post 调用添加额外的“json”参数(最后一个 dataType 参数),但这不会改变数据的编码方式。

问题“我是否应该将 JSON 数据发送到服务器?”独立于 $.post,您应该根据项目中现有的要求自行回答该问题。有时这是解决方案架构的问题。有时你必须选择一种特殊的方式。

例如,对于 Microsoft ASMX Web 服务,存在一些重要的限制。例如,您希望从 Web 服务传递 JSON 数据,以便能够轻松处理 JavaScript 中的数据。因此,您希望在服务器端有一个方法,该方法具有一些输入参数并返回 JSON 作为输出。如果 ASMX Web 服务您必须将所有输入参数作为 JSON 编码数据发送到 Web 服务方法,以便能够从 Web 服务返回 JSON 数据,但 ASMX Web 服务会为您解码/编码数据并且您不需要在服务器端手动编码/解码 JSON。

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 How do I build a JSON object to send to an AJAX WebService? 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()) }, ...);

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

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文