AJAX帖子到ASP.Net网站缺少参数

时间:2011-07-28 13:01:02

标签: jquery asp.net ajax

我创建了一个IHttpAsyncHandler,我尝试使用jQuery调用AJAX。呼叫成功,但我无法在服务器上找到我的参数。

以下是AJAX调用:

function deleteViewModel(typename) {
    var data = {
        "viewModel": typename,
        "operation": "delete"
    };

    $.ajax({
        type: "POST",
        url: "<%= GetAppRoot() %>/viewModelGeneration.ashx",
        contentType: "application/json",
        cache: false,
        data: JSON.stringify(data),
        beforeSend: function (xhr, settings) {
            $("[id$=processing]").dialog();
        },
        success: function (data) {
            alert('Hey, I succeeded.');
        },
        error: function (xhr, status, err) {
            alert('Play a sad trombone and frown.');

        },
        dataType: "json"
    });
}

调用是在服务器上完成的,由我的处理程序处理,但我没有看到viewModeloperation参数:

public void ProcessRequest(HttpContext context)
{
    // Problem is here - no parameters!
    var viewModelName = context.Request.Params["viewModel"];
    var operation = context.Request.Params["operation"];

    // Other stuff...

    GenerateResponse(context.Response, jsonResp);
}

我打开Fiddler来查看从客户端发送的请求,在我看来参数包括在内:

POST http://localhost:4638/admin/viewModelGeneration.ashx HTTP/1.1
Host: localhost:4638
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:4638/admin/Admin/ResetViewModels.aspx
Content-Length: 123
Cookie: ASP.NET_SessionId=ifmof1ole4yv20jr0frqc0lk; .ASPXFORMSAUTH=836B7EEC539B1304126C156CA20A925DD4FF832E628C807A1CA9DCD00833BDFF36D73C39B9CCFE6EA15CF9FED95157A1CA5F07D588F04A8AFE68ABDBBA82FE9FF8507CB2B471340917616818334BCF0D958CB231A1CA3B9D91B05F2897C44663B5E86FC2FFDFE3C325AB66EC3124144F87B6FC8D3F6C7F92F2FEE745EA71EB333D18E35A7FFA992F8F52FEE509043236
Pragma: no-cache
Cache-Control: no-cache

{"viewModel":"Rds.ViewModels.Updaters.RegionViewModelUpdater, Rds.ViewModels","operation":"delete"}

我不确定他们在服务器上遇到的是什么。任何想法都将不胜感激。

更新:

有人向我建议Request.Params只支持表单编码数据。我更新了我的AJAX调用,但仍然没有参数服务器端:

function updateViewModel(typename, operation) {
    var parms = {
        "viewModel": typename,
        "operation": operation
    };

    $.ajax({
        type: "POST",
        url: "/admin/viewModelGeneration.ashx",
        contentType: "application/json",
        cache: false,
        data: parms,
        beforeSend: function (xhr, settings) {
                $("[id$=processing]").dialog();
        },
        success: onSuccess,
        error: onError
    });
}

3 个答案:

答案 0 :(得分:3)

它适用于表单编码数据,但为此,您不仅需要删除dataType,还需要删除contentType属性。通过这个电话,成功:

function updateViewModel(typename, operation) {
    var parms = {
        "viewModel": typename,
        "operation": operation
    };

    $.ajax({
        type: "POST",
        url: "<%= GetAppRoot() %>/viewModelGeneration.ashx",
//      contentType: "application/json",
        cache: false,
        data: JSON.stringify(parms),
        beforeSend: function (xhr, settings) {
            $("[id$=processing]").dialog();
        },
        success: function (data) {
            alert('Hey, I succeeded.');
        },
        error: function (xhr, status, err) {
            alert('Play a sad trombone and frown.');
        }
//      dataType: "json"
    });
}

答案 1 :(得分:1)

我有一个类似的问题,我不得不简单地声明

var dataString = JSON.stringify(data)

在使用

传递dataString之前
data: dataString,

给它一个bash。至少你可以在Firebug中调试脚本,并确保dataString已经填充了一个预期的对象。

答案 2 :(得分:0)

尝试

data:{viewModel:'typename',operation:'delete'},