jquery $.post 第二个参数。 - json 还是查询字符串?
$.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,这两种方式在实践中都非常接近。函数
$.post
或$.ajax
将以相同的方式对发布的数据进行编码。如果您想发布 JSON 数据,您应该使用一些 JSON 编码器另外对数据值进行编码。请参阅如何我是否构建一个 JSON 对象以发送到 AJAX WebService? 作为示例($.post
是$.ajax
的缩写形式,因此所有这些用$.ajax
描述,并且$.post
也正确)在上面的代码中,我使用 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
)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 lastdataType
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.