jQuery 1.4對於有效的JSON響應文本非常挑剔。以下是無效的JSON(和最有可能您的問題)
{foo:'bar'}
// Strict JSON requires quoted attributes - and the quotes must be doubles!
{"foo": "bar"}
This blog post提及使用「文本」,而不是如果你不能修復服務器端JSON容易解決方法。適用於你的函數你:
function save_edit(point_id) {
var data = {};
data.id = point_id;
$.post("/foo", data, function(responseText) {
var responseData = eval("("+responseText+")");
$("#value" + point_id).show();
$("#editval" + point_id).hide();
}, "text");
}
此外,你將能夠處理錯誤的情況下做這樣的事情:
function save_edit(point_id) {
var data = {};
data.id = point_id;
var $edit = $("#editval"+point_id);
var $view = $("#value"+point_id);
$.ajax({
method: "POST",
url: "/foo",
data: data,
success: function(responseData) {
$view.show();
$edit.hide().find('.error').remove();
},
error: function(XHR, textStatus, errorThrown) {
var $err = $edit.find(".error");
if ($err.length == 0) { $err = $("<div class='error'/>").appendTo($edit); }
$err.text("Error: "+textStatus);
}
});
}
jQuery的1.4是非常嚴格的,但是jResig提供了一個插件,如果你想運行1.4並仍然提供1.3.2兼容性 - 鏈接:http://github.com/jquery/jquery-compat-1.3 – MJJames 2010-01-25 23:26:19
我的問題是,服務器使用單引號(')而不是雙引號(「) 。它返回的形式是{'foo':'bar'}而不是{「foo」:「bar」}。一旦我解決了這個問題,javascript就可以工作了。 – eggplant 2010-01-26 03:09:49