PDA

View Full Version : Json is not getting passed properly in AJAX Post Request



AJAIN32
15 Oct 2014, 4:00 AM
Hi,

I am using a AJAX POST request in a controller and i am passing one complex object and i am giving call to a MVC Controller.

I am not able to get the object in the method of controller it is always null, i tried by replacing the Passing parameter with string and i found that json string is having extra escape characters (/) with each property.

So can anybody please tell me why these extra characters is being added in Json String, i am using JSON.stringify. and i think because of these escape character it is not getting deserialized at controller side. Any idea ??

Thanks,
Ankit

skirtle
17 Oct 2014, 12:18 AM
How are you passing the data to request?

If you use the jsonData option you don't need to call stringify.

AJAIN32
30 Oct 2014, 9:44 PM
Thanks skirtle !

I am using Params configuration for passing the data. for eg.

params : {
objectToPass : Object1
}

Here objectToPass is name of parameter being used in controller's method and Object1 is the Json Data that i am passing.

At controller's side when i am using Object i am getting null whenever the method is called, and if i am using string parameter i am getting Json String but with some escape characters with each tag.

Please suggest.

Thanks,
Ankit

skirtle
30 Oct 2014, 10:10 PM
I assume the extra characters you mentioned are \, not /. Something like this?


"{\"key\":\"value\"}"

That indicates the value has been JSON encoded twice.

I suggest breaking this down into two parts. Open the dev tools and inspect the request being sent to the server. Focus on getting that request correct. Don't worry about whether the server handles it correctly, just get the request right. Once you're sending the right request you can switch your focus to getting the server to process it correctly.

You'll have to provide some concrete details of what you're doing if you need someone to help you find where exactly the problem is occurring.

AJAIN32
31 Oct 2014, 12:29 AM
Thanks for your prompt reply !!

What i am using in ajax request-

let say my object is -
var object1 = {
name : 'test1',
value : 'test1',
obj1 : null
};
object1.obj1 = {
name1 : 'test1',
value1 : 'test1'
}

object1 = JSON.stringify(object1);

and i am passing this object in the request.

Ext.ajax.request({

url : '..\..\Home\AddUser',
method : 'POST',
params : {
objectToPass : object1
},
success : function(response)
{

},
failure: function()
{

}
});

At controller side i have similar object that is coming null.

As you suggested i am looking at request in fiddler and that is incorrect its going like -

objectToPass : test1
objectToPass : test1
objectToPass : [object object]

So its not getting maped to object, so any suugestion to map it in a proper way ?

Thanks,
Ankit

skirtle
31 Oct 2014, 12:36 AM
Fiddler seems like overkill when you could just use the tools built into the browsers.

That said, the output you've posted seems to be from the case where you haven't passed the value through stringify. What do you see when you have passed it through stringify?

AJAIN32
31 Oct 2014, 12:41 AM
Thanks !!

Yes, I have tried with stringify but in the request body i have nothing to see. so its not passing the data correctly.

Any required configuration i have missed ?

Thanks,
Ankit

skirtle
31 Oct 2014, 12:55 AM
You said previously that you were receiving the data on the server but with excess escaping. That's the request I'm interested in. There's no way it can be blank if the server's getting the data.

AJAIN32
31 Oct 2014, 1:14 AM
Ya its my mistake,

I get the data with escaping characters on server if Parameter in the controller's method is string, if object then it is null.
From request prospective i checked the request body and its like -

objectToPass = %7B%22name%22%3A%22Test%22%2C%22value ... and so on till the last value in this object.

I am not sure if it goes like this or it is causing issue or not.
Anything missing to send the request correctly ?

Thanks,
Ankit

skirtle
31 Oct 2014, 1:19 AM
That looks correct to me, what you posted doesn't contain any extra escaping characters.

AJAIN32
31 Oct 2014, 1:42 AM
Ok, But its not mapping to the object at server side. and when i am using a string to get the request data then it is showing me data with escaping characters.
is it getting converted in json again on controller side ? I am using Asp.Net MVC 4 controller.

Any steps to get the issue at server side ?

Thanks,
Ankit

skirtle
31 Oct 2014, 1:58 AM
What do you mean by 'showing me data with escaping characters'?

AJAIN32
31 Oct 2014, 2:21 AM
My Controller's method is -

[HTTPPOST]
public bool AddUser(User objectToPass)
{

}
In this case i am getting null in objectToPass Parameter.

and if i write like this -

[HTTPPOST]
public bool AddUser(string objectToPass)
{

}
I am getting objectToPass as - "{\"name\":\"Test\",\"value\":"Test\",\"obj1\":{\"name1\":\"Test1\",\"value1\":\"Test1\"}}"

As you said the request is correct then it should be mapped to object at server side.
Any way to check that what is missing in request ?

Thanks,
Ankit

skirtle
31 Oct 2014, 2:50 AM
I'm not familiar enough with ASP .NET to comment.

AJAIN32
31 Oct 2014, 3:30 AM
Ok, i will look more in that.

Thanks skirtle for your support !!

Thanks,
Ankit