Not able to post JSON Data to MVC Action Method using $.AJAX but I get a Post 200 OK Response

Sandeep

I am trying to post some stringified data to an action method in my MVC controller. Below is the piece of JS Code

function SaveStateForGrid(pGridId, pIsAysnc) {
            var grid = $('#' + pGridId).data("kendoGrid");                
            var tGridState = kendo.stringify(grid.getOptions());
            $.ajax({
                url: '/Home/SaveGridState',
                type: 'POST',
                dataType: 'json',
                async: pIsAysnc,
                data: { pGridState: tGridState, pGridName: pGridId },
                success: function (data) {

                    console.log("Data Saved Successfully");
                },
                error: function (jqXHR, textStatus) {
                    console.log("Error while saving grid data " + pGridId);
                }
            });
        }

I am trying to save gridstate info into the db and each grid in my app has different grid state info.

This works sometimes - as in, the post hits the controller method "SaveGridState" if I keep a breakpoint there and so on. On other occasions the breakpoint is not hit and the data is not saved in DB but the code does flow to "success" AJAX callback - no errors and I get a 200POST OK response in the network tab of chrome debugger.

I looked up the data being sent and figured out that on the occasions it fails, there is something about the data being posted which is causing it. Specifically the first param - "tGridState" - a stringified KendoGrid config data. If I replace this data from other successful calls, there is no issue. I have compared the data on different viewing tools and am not able to understand what in the data is breaking this.

Attaching a link to the zip file which contains both "Valid" and "Invalid" data. Any help pls ?

https://www.dropbox.com/s/whfglyk607bnd04/Downloads.zip?dl=0

Sandeep

Few months on from posting this - the module in question was on freeze.

I discovered that the stringified gridstate information (grid config data) included JS functions for grid related events which was causing issues in the stringified data. In fact stringification strips such functions from an object.

Somehow this was preventing me from hitting the breakpoint on the server side even if the data was posted "apparently".

So I wrote a JS function to strip all unnecessary data from my gridstate config data before stringification - and then the post works perfectly.

Voila !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

json string on POST to MVC action method using AJAX is null

I can't get the object from action as Json Formatting using ajax post in mvc core

How to post the viewmodel to action method using JQUERY AJAX in MVC

AJAX, GET ok, POST not

Use jQuery to post data to MVC action method

With JS fetch , Post data to MVC Action Method

Post method giving 200 response but in DB data is stored as null

Receive HTTP post data and return ok(200)

Cant display json data into html using Jquery Ajax post method

get response when i call the post method

Using $.ajax to POST XML to MVC Controller method

jQuery Ajax PHP POST method with response array[] instad sent data. How to get send values in response?

Send JSON data via POST (ajax) and receive json response from Controller (MVC)

How do I POST raw JSON data without using AJAX?

How do I POST JSON data using jQuery AJAX

iOS post data to get JSON response

How to pass data from dropdown list to action method to save data using post jquery ajax call

Spring RestTemplate exchange POST HttpClientException with any non 200 OK response

MVC Post Action method not firing

Laravel Ajax response post method

JSONP ajax call executes error method its response is 200 ok

JSON not deserialized to object when doing AJAX post to MVC action

MVC5 controller action not called from JSON AJAX Post

XSS validation from MVC action from JSON ajax post

$_POST of php file doesn't work but $_GET yes. Ajax calls it, returns 200 OK but $_POST into php file is empty

How I can open popup window from controller action using form post method in mvc

How can I AJAX POST this complex type to my MVC action?

Not able to post form data to action url

Post data from angularjs to MVC action method which returns a view