2010-10-02 119 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