私は CakePHP で jQuery-1.4.2 を使用しています。数日前にはこれが正常に機能していましたが、現在は機能していません。何かを変えた覚えはありません。JavaScript から生成された URL を取得してブラウザに直接入力すると、ブラウザで結果を確認できますが、JavaScript は null の戻り値を取得しています。アイデアを持っている人はいますか?
<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"}}
なぜnullデータを取得し続けるのか疑問に思っています。何か案は?クロス ドメイン クエリを実行していません。ドメインのスクリプトのみを照会しています。
編集:
JSON出力を次のように表示するように変更しました{"Votes":{"votecast":"up","sumvotes":"1","totalvotes":"1"}}
.jsonlint.comは有効なJSONであると言っていますが、それでも機能せず、jQueryは結果がnullであると言っています.