jQuery $.get 不再返回数据
我正在使用 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 说结果为 null。
I am using jQuery-1.4.2 with CakePHP and a few days ago I had this working fine but now it isn't. I don't remember changing anything. I am taking the URL generated from the javascript and putting that directly into the browser and I am able to see the results in my browser however javascript is just getting a return value of null. Anyone have 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>
Output from debug console in browser is
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
The output my php script does is
{"Votes":{"votecast":"up","sumvotes":"1","totalvotes":"1"}}
I'm wondering why I keep getting null data. Any ideas? I am not doing a cross domain query. I am only querying a script on my domain.
EDIT:
I have changed the JSON output to display as {"Votes":{"votecast":"up","sumvotes":"1","totalvotes":"1"}}
and jsonlint.com says it is valid JSON however it still does not work and jQuery says the result is null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现您在脚本开头指定了 URL:
您是否违反了 同源政策? IE。您只能对原始服务器进行 ajax 调用。
这会给您所描述的症状:空白响应,但通过浏览器手动尝试时该 URL 有效。
I see that you are specifying the URL in the start of your script:
Could it be that your are violating the same origin policy? Ie. that you can only do ajax calls to the originating server.
That would give you the symptoms you describe: a blank response, but the URL works when tried manually through the browser.
我不确定为什么它不返回数据,但您可能想确保 url $.get 是您认为的那个。例如,您可以使用
alert(url + "view/" + $(".votediv").attr("id") +".json");
- 当然您也可以看到在萤火虫中。我相信
$.get
有时可以返回“成功”状态,即使它实际上没有加载网址。I'm not sure why it's not returning data, but you might want to make sure the url $.get is going to is the one you think it is. You could use
alert(url + "view/" + $(".votediv").attr("id") +".json");
for example - of course you can also see that in Firebug.I believe
$.get
can sometimes return a status of 'success' even if it's not actually loading a url.