I have this simple ajax request on the client side:
var name = $("#txtNewsletterName");
var email = $("#txtNewsletterEmail");
$.ajax({
url: "/Handlers/Handler.ashx",
contentType: "application/json; charset=utf-8",
type: "POST",
dataType: "json",
data: {
op: "register_to_newsletter",
name: name.val(),
email: email.val()
},
async: true
});
and this code on the C# server side:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
switch (context.Request["op"])
{
case "register_to_newsletter":
string recipientName = context.Request["name"].Trim();
string recipientEmail = context.Request["email"].Trim();
break;
default:
break;
}
}
The problem is that the data from the request is not passed to the server, so the context.Request["op"]
, context.Request["name"]
and context.Request["email"]
are null.
I've also checked context.Request.Form.AllKeys
and it's string[0]
So obviously the data does not get to the server.
When checking the Network tab in chrome debugger I see that there are 2 requests sent so I've added a screenshot of the Network data from chrome debugger: