2010-10-02 86 views
0

我使用jQuery-1.4.2和CakePHP,前几天我有这个工作正常,但现在不是。我不记得改变任何事情。我正在从JavaScript生成的URL并将其直接放入浏览器,我能够在浏览器中看到结果,但JavaScript只是返回null值。任何人都有IDE吗?从调试控制台

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

回答

3

我看到你指定在你的脚本开始的网址:

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

难道说你违反了该same origin policy? IE浏览器。您只能对原始服务器执行ajax调用。

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

+0

正如我在我的问题中提到的那样,我表示我正在查询服务器上的脚本,而没有执行任何跨域查询。 – Bot 2010-10-05 16:04:38

+0

看起来你是对的。原来,如果我在我的浏览器中使用www,因为我没有在我的jQuery url中指定www,它会导致该问题。为什么www会违反相同的原产地政策?有没有一个jQuery函数来获取浏览器基础网址,所以我可以使用它? – Bot 2010-10-07 16:55:52

+0

同样的原点也会触发不同的子域。您可以使用相对URL,只需转到“/ vote /”即可。 – Magnar 2010-10-07 19:21:20

0

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

我认为$.get有时可以返回'成功'的状态,即使它实际上并没有加载一个url。

+0

我已经在我上面的帖子中说过我已经通过输出url并手动测试来确认url是正确的。 – Bot 2010-10-04 16:34:01