我使用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