jQuery $ .toJSON不让我发帖?

时间:2012-01-30 19:29:24

标签: php jquery

我有以下

    var storage = [], obj;
    $('form input[type=hidden]').each(function(){
        obj = {};
        obj[this.name] = this.value;
        obj["spot"] = this.className
        storage.push(obj);
    });

    storage = $.toJSON(storage);
    console.log(storage);

    $.post('storage/', storage, function(data) {
        if(data == "true") {
            //window.location.href = href;
        }else{
            alert("An error has been encountered, Blah has been notified, please try again later");
        }
    });

并且在PHP中我有一个简单的<?php print_r($_POST); ?>并且它正在打印Array( )它似乎没有发布json编码的结果。

这让我疯了,我不知道发生了什么事。有什么帮助吗?

PS。我使用http://code.google.com/p/jquery-json/作为json编码器。

1 个答案:

答案 0 :(得分:2)

jQuery的ajax数据选项需要一个对象或一个查询字符串。你传递的是一个json字符串,它没有预料到。试试这个:

$.post('storage/', {storage: storage}, function(data){...});

并在php中,使用$_POST["storage"]

访问该值

编辑:此外,data == "true"应该是/true/i.test(data),只要你的php返回任何隐藏的字符,例如空格标签或返回。