Jquery Ajax with Parameters

Starr Duskk
Starr Duskk used Ask the Experts™
on
I'm trying to post to an HttpHandler ashx file with parameters.

function sendClickData(methodName,paramValue) {
    
    try {
        // create web service param object
        var passUrl = "SetGridDefaults.ashx";

        // save data to web service using an ajax post method
        $.ajax({
            type: "POST",
            url: passUrl,
            data: "MethodName=SetDefaultGridView",
            success: function (data) {

            },
            error: function (error) {
                alert(error);
            }
        });
    } catch (e) { }

}

Open in new window


In the data row, I am passing values to be retrieved as query strings:

In my ProcessRequest, I am retrieving the value:

  Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim method As String = context.Request.QueryString("MethodName").ToString()

Open in new window


When I run it, it gives me an error that the MethodName is null on the request.querystring line of code:
>>An exception of type 'System.NullReferenceException' occurred

This code was from an original article here:
original code

The only thing I see different is that it says to include an older version of jquery - version 1, whereas I am using version 3.

Also, in addition, I would like to pass two values if possible.

And yes, I know I can use a web service or API, but I was hoping I could use this, since it's just a very simple request.

Thanks!

Starr
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
jQuery version is not the issue.

You are doing a queryString retreival on a POST. With a POST data is sent in the body of the request whereas the queryString refers to URL parameters.
So, either change your AJAX to a GET request
OR
Move your parameters into the URL
So Either
function sendClickData(methodName,paramValue) {
        try {
        // create web service param object
        var passUrl = "SetGridDefaults.ashx";

        // save data to web service using an ajax post method
        $.ajax({
            type: "GET",
            url: passUrl,
            data: "MethodName=SetDefaultGridView",
            success: function (data) {
            },
            error: function (error) {
                alert(error);
            }
        });
    } catch (e) { }
}

Open in new window

Or
function sendClickData(methodName,paramValue) {
        try {
        // create web service param object
        var passUrl = "SetGridDefaults.ashx?MethodName=SetDefaultGridView";

        // save data to web service using an ajax post method
        $.ajax({
            type: "POST",
            url: passUrl,
            data: "",
            success: function (data) {
            },
            error: function (error) {
                alert(error);
            }
        });
    } catch (e) { }
}

Open in new window

To pass more than one parameter - separate them with &'s
MethodName=SetDefaultGridView&secondParam=value&thirdParam=Value2

Open in new window

Starr DuskkASP.NET VB.NET Developer

Author

Commented:
oh perfect! thanks!
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial