通过jquery ajax发送json无法正常工作

时间:2012-01-23 20:36:29

标签: php jquery ajax

我正在发送一个AJAX请求并发送一些JSON:

 $(function() {
     var json = [{"id":"1", "area":"south"}, {"id":"2", "area":"north"},{"id":"3", "name":"east"},{"id":"1", "name":"west"}];

     jQuery.ajax({
         url: "index.php",
         type: "POST",
         data: {areas: JSON.stringify(json) },
         dataType: "json",
         beforeSend: function(x) {
             if (x && x.overrideMimeType) {
                 x.overrideMimeType("application/j-son;charset=UTF-8");
             }
         },
         success: function(result) {
         alert(result);
         }
     });

然后我试图在另一端使用json_decode将其解码为对PHP有用的东西:

<?php
    if(isset($_POST['areas'])) {
        $json = $_POST['areas'];
        $obj = json_decode($json);
        var_dump($obj);
        exit;
    }
?>

AJAX帖子变得很好,如果我在if(isset$_POST)内放回一个回声,我就会回来,所以它似乎已经被发送了。但我只从代码中返回Null。谁能看到我错过的东西?

4 个答案:

答案 0 :(得分:1)

怎么样:

$obj = json_decode($json, true);

也不应该x.overrideMimeType("application/j-son;charset=UTF-8");实际上是x.overrideMimeType("application/json;charset=UTF-8");

答案 1 :(得分:1)

我相信您可以直接将json数组传递给AJAX函数并跳过JSON.stringify函数调用:

$(function() {
     var json = [{"id":"1", "area":"south"}, {"id":"2", "area":"north"},{"id":"3", "name":"east"},{"id":"1", "name":"west"}];

     jQuery.ajax({
         url: "index.php",
         type: "POST",
         data: {areas: json },
         dataType: "json",
         beforeSend: function(x) {
             if (x && x.overrideMimeType) {
                 x.overrideMimeType("application/j-son;charset=UTF-8");
             }
         },
         success: function(result) {
             alert(result);
         }
    });
});

更新

如果这是运行$_POST['area']之前PHP脚本中json_decode变量的输出:

Array (
    [0] => Array ( [id] => 1 [area] => south )
    [1] => Array ( [id] => 2 [area] => north )
    [2] => Array ( [id] => 3 [name] => east ) 
    [3] => Array ( [id] => 1 [name] => west )
)

然后您不需要运行json_decode,因为您已经拥有了所需的对象。

答案 2 :(得分:1)

你不必使用stringify jQuery会照顾它。试试这个。

$(function() {
     var json = [{"id":"1", "area":"south"}, {"id":"2", "area":"north"},{"id":"3", "name":"east"},{"id":"1", "name":"west"}];

     jQuery.ajax({
         url: "index.php",
         type: "POST",
         data: {areas: json },
         dataType: "json",
         beforeSend: function(x) {
             if (x && x.overrideMimeType) {
                 x.overrideMimeType("application/j-son;charset=UTF-8");
             }
         },
         success: function(result) {
         alert(result);
         }
     });
});

答案 3 :(得分:0)

$(function() {
     var json = [{"id":"1", "area":"south"}, {"id":"2", "area":"north"},{"id":"3", "name":"east"},{"id":"1", "name":"west"}];

 jQuery.ajax({
     url: "index.php",
     type: "POST",
     data: {areas: JSON.stringify(json) },
     //dataType: "json",
     beforeSend: function(x) {
         if (x && x.overrideMimeType) {
             x.overrideMimeType("application/j-son;charset=UTF-8");
         }
     },
     success: function(result) {
     alert(result);
     }
 });

您正在转储不是json的响应,dataType参数告诉函数期望的响应类型。如果你没有放任何东西,它会给你你所期望的,PHP脚本中的var_dump。 (这已通过您发布的代码进行了验证。)