无法将数据$ .ajax传递给ashx处理程序

时间:2013-10-31 09:37:08

标签: jquery asp.net handlers

我有这段代码调用我的处理程序,但我无法在我的处理程序中接收数据,你能帮忙吗?

// 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);
                }

结果总是一样,我尝试了其他选项,但总是有相同的结果:对象引用没有设置为对象的实例。

1 个答案:

答案 0 :(得分:0)

您正在以JSON格式发送数据,但正在尝试通过查询字符串进行读取。

像这样使用

$("#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
    });
}

现在您可以在查询字符串中接收数据。

相关问题