1
一切工作正常,並整數/浮動花花公子,但是當我輸入一個字符串到文本框中的數據永遠不會收到的C#方法GetData
。jQuery.ajax()僅適用於整數
ASP代碼
<asp:TextBox id="txtBoxVersion" runat="server"></asp:TextBox>
<asp:ImageButton id="iconVersionSave" class="iconSave" runat="server" imageUrl="Resources/iconSave.png" OnClientClick="asyncServerCall(document.getElementById('txtBoxVersion').value); return false;"></asp:ImageButton>
JQUERY
function asyncServerCall(userData)
{
jQuery.ajax(
{
url: 'SurveyUpload.aspx/GetData',
type: "POST",
data: "{\"userData\":" + userData + "}", //Data to be sent to the server !!WARNING!! Field Name must match C# parameter name
contentType: "application/json; charset=utf-8", //when sending data to the server
dataType: "json", //The type of data that you're expecting back from the server.
success:
function (data)
{
alert('Success');}
}
});
}
C#
[WebMethod()]
public static Boolean GetData(String userData)
{
System.Diagnostics.Debug.WriteLine(userData); //DEBUGGING
return true;
}
我習慣於用java後端做ajax,所以這可能太天真了。但是,你的花括號是否有引號的理由嗎?我認爲這意味着你將發送字符串「{」userData「:something}」,而不是使用參數userData設置爲POST的POST。 – BostonJohn
不能給你一個詳細的答覆,但我知道它不工作沒有引號括起來括號括起來......也許別人可以在這樣的陳詞? –