0

我将 jQuery-1.4.2 与 CakePHP 一起使用,几天前我的工作正常,但现在不行。我不记得改变了什么。我正在获取从 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 说结果为空。

4

2 回答 2

3

我看到您在脚本的开头指定了 URL:

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

会不会是你违反了同源政策?IE。您只能对原始服务器进行 ajax 调用。

这会给你你描述的症状:一个空白的响应,但是当通过浏览器手动尝试时 URL 有效。

于 2010-10-04T18:33:56.030 回答
0

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

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

于 2010-10-02T02:09:06.633 回答