2011-08-31 36 views
2

我想將JavaScript變量的內容傳遞給服務器進行處理。我可以傳遞靜態字符串沒有問題,但是當我傳遞一個包含字符串的變量時,WebMethod不會被調用。這裏是我的代碼: (客戶端)jQuery.ajax「數據」參數語法

function expand(checkbox) 
{ 
    var selectedrow = checkbox.parentNode.parentNode; 
    var rowindex = selectedrow.rowIndex; 
    var parent = document.getElementById("parentTable"); 
    var NextRow = parent.rows[rowindex + 1]; 

    var cols = selectedrow.cells[1]; 
    var ID = cols.firstElementChild.attributes.value; 


    $.ajax({ 
     type: "post", 
     url: "Playground.aspx/childBind", 
     data: "{sendData: ID}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (result) { alert("successful!" + result.d); } 
    }) 

    NextRow.style.visibility = "visible"; 
} 

(服務器)現在

[WebMethod] 
    public static string childBind(string sendData) 
    { 
     return String.Format("Hello"); 
    } 

,如果我嘗試的數據: 「{送出數據: 」OK「}」,將WebMethod被調用和返回一個響應。我的語法錯了嗎?

+0

在這裏工作: http://stackoverflow.com/questions/7262940/webmethod-not-being-called- – Seraph812

回答

3

您不必將它作爲字符串傳遞。由於ID是一個JavaScript變量,因此您必須傳遞其值。當你傳遞數據爲"{sendData: ID}"時,它不會通過ID的值。

試試這個

data: { sendData: ID } 
+0

@ Seraph812 - Hope this h愛上你。 – ShankarSangoli

+0

這似乎不起作用,雖然它確實讓我在其他地方發現了錯誤。在javascript中稍作修改後,我現在存儲了正確的值。 var ID = cols.firstElementChild.defaultValue; 但是,服務器端Web方法仍未被調用。我注意到,如果我註釋掉「contentType」和「dataType」行,客戶端的「成功」語句被調用。 「ID」包含實際上是GUID的隱藏字段的字符串值(但存儲時是字符串)。服務器端webmethod需要一個字符串。 – Seraph812

+0

你做了什麼修改?你的意思是你仍然在傳遞'data:「{sendData:ID}」'它可以工作嗎? – ShankarSangoli

3

你被髮送,而不是一個對象(而不是"{sendData: ID}"{sendData: ID})的字符串。而你發送的數據不是JSON。所以刪除contentType行並更改數據行。你應該重新寫爲:

$.ajax({ 
    type: "post", 
    url: "Playground.aspx/childBind", 
    data: {sendData: ID}, 
    dataType: "json", 
    success: function (result) { alert("successful!" + result.d); } 
}) 

你也可以這樣寫,如果你想發送JSON:

$.ajax({ 
    type: "post", 
    url: "Playground.aspx/childBind", 
    data: $.getJSON({sendData: ID}), 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    success: function (result) { alert("successful!" + result.d); } 
}) 
+0

現在看起來好像正確的數據,但服務器端web方法仍然沒有被調用。我注意到,如果我註釋掉「contentType」和「dataType」行,客戶端的「成功」語句被調用。 「ID」包含實際上是GUID的隱藏字段的字符串值(但存儲時是字符串)。服務器端webmethod需要一個字符串。 – Seraph812

+0

您應該刪除contentType行,因爲您沒有將數據作爲json發送(您只是以json的身份接收數據)或者可以在數據周圍添加jQuery.getJSON() –

0

由於您使用jQuery的運行這些測試的答案:

var ID = 45; 

var x = "{sendData: ID}"; 

alert(jQuery.isPlainObject(x)); // false 

var y = { sendData: ID}; 

alert(jQuery.isPlainObject(y)); // true 

這裏是的jsfiddle:

http://jsfiddle.net/3ugKE/

0

您正在傳入'ID'作爲字符串而不是變量。 您只需刪除數據對象的引號即可。

進一步閱讀JSON和JavaScript對象:

http://www.json.org/

http://www.w3schools.com/js/js_objects.asp

+0

看起來好像正確的數據現在就位,但服務器端web方法仍未被調用。我注意到,如果我註釋掉「contentType」和「dataType」行,客戶端的「成功」語句被調用。 「ID」包含實際上是GUID的隱藏字段的字符串值(但存儲時是字符串)。服務器端webmethod需要一個字符串。 – Seraph812

0

您可以使用此代碼:

$.ajax({ 
     type: "post", 
     url: "Playground.aspx/childBind", 
     data: JSON.stringify({sendData: ID}), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (result) { alert("successful!" + result.d); } 
    })