我使用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說,結果爲空。
正如我在我的問題中提到的那樣,我表示我正在查詢服務器上的腳本,而沒有執行任何跨域查詢。 – Bot 2010-10-05 16:04:38
看起來你是對的。原來,如果我在我的瀏覽器中使用www,因爲我沒有在我的jQuery url中指定www,它會導致該問題。爲什麼www會違反相同的原產地政策?有沒有一個jQuery函數來獲取瀏覽器基礎網址,所以我可以使用它? – Bot 2010-10-07 16:55:52
同樣的原點也會觸發不同的子域。您可以使用相對URL,只需轉到「/ vote /」即可。 – Magnar 2010-10-07 19:21:20