代码之家  ›  专栏  ›  技术社区  ›  Bot

jquery$.get不再返回数据

  •  0
  • Bot  · 技术社区  · 15 年前

    我在用jquery-1.4.2和cakephp,几天前我还可以很好地使用它,但现在不行了,我不记得做了什么改动。我将获取从javascript生成的URL并将其直接放到浏览器中,我可以在浏览器中看到结果,但javascript的返回值为空。有人有IDES吗?

    <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说结果为空。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Magnar    15 年前

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

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

    可能是你违反了 same origin policy ?也就是说,您只能对发起服务器进行Ajax调用。

    这将给您描述的症状:一个空白的响应,但是当通过浏览器手动尝试时,URL可以工作。

        2
  •  0
  •   Alex King    15 年前

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

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