我需要将JSON数据传递到服务器。数据如下:

{"method":"like","data":{"type":1,"id":123}}

我需要自动完成,如果我想用一个表格,可以这样做:
<script>
jQuery(document).ready(function(){
jQuery("#form").submit();
});
</script>

但我需要用$POST以这种格式传递数据。怎么能做到?

最佳答案

请参阅jquery的load()或更好的(post()如果不需要加载任何内容到您正在处理的页面),以便使用POST传递json对象
如果不知道如何将字符串/数组转换为json,请使用phpsjson_encode
例子:

$(document).ready(function(){
    $.post("script.php", { <?php echo $string; ?> },
       function() {
         alert("Your test.php page received the string!);
       });
});

这样当页面加载时,$string会自动加载(如果用户打开了javascript)。

09-25 17:12
查看更多