0
我有以下代碼工作在POC沒有變量和硬編碼的一切。在引入變量以準備使用此通道後,它停止工作。可能是,我沒有看到語法錯誤,或者(我不敢說)這個POC無法支持這樣的請求?jquery.ajax調用一個.aspx網頁方法
從我的aspx頁面sendEmail.aspx
[System.Web.Services.WebMethod]
public static string SendMyEmail(string EmailFromAddress, string EmailFromName, string EmailSubject, string EmailBody)
{
return "data from server: " + Environment.NewLine +
"EmailFromAddress = " + EmailFromAddress + Environment.NewLine +
"from = " + EmailFromName + Environment.NewLine +
"from = " + EmailSubject + Environment.NewLine +
"from = " + EmailBody;
}
<script type = "text/javascript">
function ShowCurrentTime() {
alert("hi");
$.ajax({
type: "POST",
url: "SendEmail.aspx/SendMyEmail",
data: '{EmailFromAddress: "mike", EmailFromName="mike", EmailSubject="email subject here", EmailBody="email body here"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
從我的HTML網頁:
<script type = "text/javascript">
function ShowCurrentTime() {
alert("hi");
$.ajax({
type: "POST",
url: "SendEmail.aspx/SendMyEmail",
data: '{EmailFromAddress: "mike", EmailFromName="mike", EmailSubject="email subject here", EmailBody="email body here"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert("all good");
alert(response.d);
}
</script>
</head>
<body style = "font-family:Arial; font-size:10pt">
<form id="form1" runat="server">
<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
onclick = "ShowCurrentTime()" />
</div>
</form>
</body>
</html>
它運作良好,當Web方法有一個嬰兒車(如下圖所示),甚至在json數據中有多個數據點:
[System.Web.Services.WebMethod]
public static string SendMyEmail(string EmailFromAddress)
{
return "good data from server: " + EmailFromAddress;
}
嘗試傳入webmethod需要的所有變量。你只是發送EmailFromAddress,嘗試添加其餘的。這個系統最有可能只傳遞1個參數,正在尋找只有該參數的方法。 – Paritosh
no-go,我原本是這樣的:data:'{EmailFromAddress:「mike」,EmailFromName =「mike」,EmailSubject =「email subject here」,EmailBody =「email body here」}', – kacalapy