开发者

AJAX post to ASP.Net web site is missing parameters

I have created an IHttpAsyncHandler that I'm trying to call using AJAX, with jQuery. The call succeeds, but I can't find my parameters on the server.

Here is the AJAX call:

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

The call comes through on the server and is handled by my handler, but I do not see either the viewModel or operation parameters there:

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

I popped open Fiddler to get a look at the request being sent from the client, and it appears to me that the parameters are included:

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"}

I'm not sure what's happening that they are not coming across on the server. Any thoughts would be appreciated.

UPDATE:

Someone suggested to me that Request.Params only supports forms-encoded data. I updated my AJAX call to this, but still no parameters server-side:

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


It works with forms-encoded data, but to do so, you not only need to remove the dataType but also the contentType property. With this call, success:

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


I had a similiar issue whereby I had to simply declare

var dataString = JSON.stringify(data)

before passing the dataString in with

data: dataString,

Give it a bash. At the least you can debug the script in Firebug and make sure that dataString has been populated with an expected object.


try with

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜