Question

I've this piece of code that calls my handler, but i cant receive data in my handler, can you help?

//CLIENT SIDE

$("#saveChanges").click(function () {
    $.ajax({
        url: "../handlers/adminSaveResults.ashx",
        type: "POST",
        data: "{ 'pinNovo': '" + "123456" + "' }",
        async: true,
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (data) {
            alert("Dados Guardados! ;)");
        },
        error: function (data) { alert("ERRO: " + data.status); },
        timeout: 15000
    });
}

//SERVER SIDE (adminSaveResults.ashx)

                try
                {
                    context.Response.ContentType = "text/json";
                    context.Response.Write(context.Request.QueryString["pinNovo"].ToString());
                }
                catch (Exception msg)
                {
                    context.Response.Write(msg.Message);
                }

And the result is allways the same, i've tried other options but allways with the same result: Object reference not set to an instance of an object.

Was it helpful?

Solution

You are sending data as JSON but are trying to read through querystring.

Use like this

$("#saveChanges").click(function () {
    $.ajax({
        url: "../handlers/adminSaveResults.ashx?pinNovo=123456",
        type: "POST",
        data: {},
        async: true,
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (data) {
            alert("Dados Guardados! ;)");
        },
        error: function (data) { alert("ERRO: " + data.status); },
        timeout: 15000
    });
}

Now you can receive data in query string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top