Reputation: 10134
I had the below code working in a POC without variables and hard coded everything. After introducing variables to prepare for using this channel it stopped working. Likely, there is a syntax error I am not seeing, or (I don't dare say) this POC is not able to support such requests?
from my aspx page 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);
}
});
}
from my html web page:
<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>
It works well when the web method has one pram (like below) even with multiple data points in the json data:
[System.Web.Services.WebMethod]
public static string SendMyEmail(string EmailFromAddress)
{
return "good data from server: " + EmailFromAddress;
}
Upvotes: 1
Views: 1887
Reputation: 847
Your ajax should look like this. Remove the '=' and replace with ':' to create proper json in the data field.
$.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);
}
});
Upvotes: 4