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