valerii.sverdlik
valerii.sverdlik

Reputation: 559

Problems with sending ajax post to controller

I have an action

[HttpPost]
    public JsonResult AddLocationsForOrder(List<BuyerOrderLocation> locations, string[] orders)
    {
        // something here
    }

and js code, which sends the request:

data = { locations: serializedLocations, orders: selector.val() };
     $.ajax({
            url: addLocationUrl,
            success: function (responseText) { Core.responceReceived(responseText, null, null); },
            error: function () { Core.showErrorNotification("Sorry, some error occured. Please contact administrator"); },
            async: false,
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(data)

        });

post details from firebug are here:

locations


"[{"id":225,"country":"United States","countryShort":"US","state":"Iowa","stateShort":"IA","city":null,"zipCode":null,"address":"Douglas, IA, USA","latitude":41.9053851,"longtitude":-93.8349976,"bounds":null,"isDeleted":false,"county":"Boone","radius":0},{"id":226,"country":"United States","countryShort":"US","state":"Iowa","stateShort":"IA","city":null,"zipCode":null,"address":"Iowa, USA","latitude":41.8780025,"longtitude":-93.097702,"bounds":null,"isDeleted":false,"county":null,"radius":0}]"


orders


[ "10440" , "10441" , "10442" ]


0  "10440"


1  "10441"


2  "10442"

And request headers:

    Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Content-Length  830
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  ASP.NET_SessionId=ewtvgwleg5or1lqctinpjv2d; .ASPXAUTH=8414A94FE30B8F8D6FE862259398F39D6F6D2EE995C9EE16549987E2E1291851788CAB75425579F61F70EBE4C7B785B07CB36773894A5B2A513966247AA5B670A25D4AE565796B449912D745EBE5E5E4AB8280902C132FC3D97C0C33BA2C2357372CD9C9EA49983DC4A8E875C6E4D653FA049EC7B0F3824666F35D3838226AA19ACEEC1B8C5716E995966787268313FEF90E2ABBAE989CA682D406EBCE361BB7
Host    local.attorneyboost
Referer http://local.attorneyboost/Order/OrderInfo/10439
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
X-Requested-With    XMLHttpRequest

As you can see, request type is "POST", and I'm "Jsoning" data before sending to the server. But in action I got a null values for parameters. What am I doing wrong? I'm already stuck on searching for mistake

Upvotes: 1

Views: 368

Answers (1)

Jorge
Jorge

Reputation: 18237

You need to specify the datatype in the request send it to the server. The datatype attribute of your called sets the format of the response of the server, to set the datatype in the format that need to send to the server you need to use the contentType attribute

Modify the ajax called with this

     $.ajax({
        url: addLocationUrl,
        success: function (responseText) { Core.responceReceived(responseText, null, null); },
        error: function () { Core.showErrorNotification("Sorry, some error occured. Please contact administrator"); },
        async: false,
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(data),
        contentType = 'application/json; charset=utf-8'
    });

You need to specify the datatype in the request send it to the server. The datatype attribute of your called sets the format of the response of the server, to set the datatype in the format that need to send to the server you need to use the contentType attribute

Upvotes: 1

Related Questions