c#: with a Handler(.ashx) get post send with javascript

user3219202 picture user3219202 · Jan 21, 2014 · Viewed 11.5k times · Source

I am trying to get a string from a textbox in a .aspx page. When I debug my site jQuery.post is able to see the input value, but when I try to get the value in my handler, he is giving me NULL back. Anyone help!!!

JS:

CompanyName = $("#company").val();
jQuery.post('/CartHandler.ashx', { 'CompanyName': CompanyName });

ASHX:

public void ProcessRequest(HttpContext context)
{
    string ImeTvrtke = context.Request.QueryString["CompanyName"];
}

Answer

Darin Dimitrov picture Darin Dimitrov · Jan 21, 2014

When you make a POST request, the value is not sent as part of the query string. So don't look in the query string for it. Retrieve it like this:

string ImeTvrtke = context.Request["CompanyName"];

Alternatively if you wanted to be sent as part of the query string then use a GET request:

jQuery.get('/CartHandler.ashx', { 'CompanyName': CompanyName });