jQuery $ .get不再返回数据

时间:2010-10-02 00:00:43

标签: cakephp jquery jquery-post

我正在使用带有CakePHP的jQuery-1.4.2,而且几天前我的工作正常,但现在却没有。我不记得改变了什么。我正在从javascript生成的URL并将其直接放入浏览器中,我能够在浏览器中看到结果,但是javascript只是返回null值。有人有想法吗?

<script type="text/javascript">
    $(document).ready(function() {
        var url = "http://mysite.com/vote/";

        $.get(url + "view/" + $(".votediv").attr("id")  +".json" ,
                function(data,textStatus, XMLHttpRequest) {
                    console.log(data);
                    console.log(textStatus);
                    console.log(XMLHttpRequest);
                    if(data.Votes.votecast != undefined) {

                        $(".vote."+data.Votes.votecast).addClass("current");
                    }
                },"json");
        $('.vote').click(function () {
            if ($(this).hasClass("current")) {
                alert("You have already voted for this option!");
            } else {

                var error = false;

                if ($(this).hasClass("up")) {

                    $.post(url + "edit/" + $(".votediv").attr("id")  +".json", {'vote': 'up'},
                        function(data) {
                            console.log(data);
                        }, "json");
                    //alert("Voted Up!");
                }
                else if($(this).hasClass("down")) {
                    $.post(url + "edit/" + $(".votediv").attr("id")  +".json", {'vote': 'down'},
                        function(data) {
                            console.log(data);
                        }, "json");
                    //alert("Voted Down!");
                }
                //removes all the votes
                $(this).parent().children().removeClass("current");

                if(!error) {
                    $(this).addClass("current");
                } else {
                    alert("There was an error");
                }
            }
            return false;
        });
    });
</script>

浏览器中调试控制台的输出是

null
success

XMLHttpRequest
    abort: function () {x&&h.call(x);
    onabort: null
    onerror: null
    onload: null
    onloadstart: null
    onprogress: null
    onreadystatechange: function () {}
    readyState: 4
    responseText: ""
    responseXML: null
    status: 0
    statusText: ""
    upload: XMLHttpRequestUpload
    withCredentials: false
    __proto__: XMLHttpRequestPrototype

TypeError: Result of expression 'data' [null] is not an object.
Failed to load resource: cancelled

我的php脚本的输出是

{"Votes":{"votecast":"up","sumvotes":"1","totalvotes":"1"}}

我想知道为什么我一直在获取空数据。有任何想法吗?我不是在做跨域查询。我只是在我的域上查询脚本。

编辑:

我已将JSON输出更改为显示为{"Votes":{"votecast":"up","sumvotes":"1","totalvotes":"1"}},jsonlint.com表示它是有效的JSON但是它仍然不起作用,jQuery表示结果为null。

2 个答案:

答案 0 :(得分:3)

我发现您在脚本的开头指定了URL:

var url = "http://mysite.com/vote/";

你是否违反了same origin policy? IE浏览器。你只能对原始服务器进行ajax调用。

这会给你描述的症状:空白的回复,但是当通过浏览器手动尝试时,URL会起作用。

答案 1 :(得分:0)

我不确定为什么它不会返回数据,但你可能想确保url $ .get将是你认为的那个。例如,您可以使用alert(url + "view/" + $(".votediv").attr("id") +".json"); - 当然您也可以在Firebug中看到它。

我相信$.get有时可以返回“成功”状态,即使它实际上没有加载网址。