2010-01-25 64 views
2

我有一個函數將數據提交給我的服務器,然後刪除編輯UI並用常規UI替換它。這在JQuery 1.3.2下完美工作,但不適用於JQuery 1.4.0。有任何想法嗎?顯示/隱藏使用JQuery 1.3.2但不使用JQuery 1.4

function save_edit(point_id) { 
    var data = {}; 
    data.id = point_id; 
    $.post("/foo", data, function(responseData) { 
    $("#value" + point_id).show(); 
    $("#editval" + point_id).hide(); 
    }, "json"); 
} 

回答

7

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); 
    } 
    }); 
} 
+0

jQuery的1.4是非常嚴格的,但是jResig提供了一個插件,如果你想運行1.4並仍然提供1.3.2兼容性 - 鏈接:http://github.com/jquery/jquery-compat-1.3 – MJJames 2010-01-25 23:26:19

+0

我的問題是,服務器使用單引號(')而不是雙引號(「) 。它返回的形式是{'foo':'bar'}而不是{「foo」:「bar」}。一旦我解決了這個問題,javascript就可以工作了。 – eggplant 2010-01-26 03:09:49

0

jQuery 1.4使用嚴格的JSON解析,如release notes中所述。您的數據是否有效JSON?