Liran Friedman Liran Friedman - 4 months ago 30
jQuery Question

C# - jquery ajax posted params not passing to server

I have this simple ajax request on the client side:

var name = $("#txtNewsletterName");
var email = $("#txtNewsletterEmail");

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();

The problem is that the data from the request is not passed to the server, so the
are null.

I've also checked
and it's

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:

  1. request #1

  2. request #2

Any idea why ?

This is the screenshot after changing the URL to lowercase to fix the redirect issue:
request #3


There is a redirect occurring, which seems to be dropping the data.

If you look at the the second screenshot you see a GET HTTP 200, but the data is no longer in the request.

The redirect is from "/Handlers/Handler.ashx" to "/handlers/handler.ashx". Maybe there's an urlrewrite in the web.config that enforces lowercase urls and does a redirect otherwise?

What if you change the url to all lowercase:

url: "/handlers/handler.ashx",

And remove the contentType setting:

contentType: "application/json; charset=utf-8",

Because you're not deserializing the data on the server, but want to send it as the default contentType application/x-www-form-urlencoded; charset=UTF-8. The dataType is for the response, the contentType for the request.