PDA

View Full Version : Object is not passing in AJAX POST request



AJAIN32
11 Sep 2014, 8:30 PM
Hi,

How we can pass a object in a ajax POST request ?

I know there are lot of examples available on internet, but i am not able to post an object.
I am using Asp.Net MVC Client application and through ajax i want to add something (for eg. UserDetails) in database, so i have a AddUser(User user) method in home controller.
similar model i am having in sencha model also, and i am passing user details as a user object in ajax but at the controller end i am not getting data :(

So can anyone please share some code of ajax post request who is passing the parameters in a object and getting data on the calling method ?

Please help !!

thanks,
Ankit

StuartAshworth
11 Sep 2014, 11:45 PM
I think you will have to serialise your data object into a JSON string and have your serverside automatically decode that into your serverside model. I believe you can do this with ASP.NET MVC but i'm afraid I don't know the specifics.

You might have more success posting a question on StackOverflow where there might be more .NET knowledge to tap into.

AJAIN32
12 Sep 2014, 12:41 AM
Thanks for prompt reply,


I have tried with Ext.encode(record) and i can see the json in the request. but its not working.

Anybody Anyidea ?

Thanks,
Ankit

AJAIN32
12 Sep 2014, 2:18 AM
I got the idea that why it data is not getting posted via ajax post call.

I am using form.getValues(); for the data, that is returning me one object of values but not in the form of model.

so the problem is - how can get the record from form that is in form of model, like we use - while we set any record by setValue(obj) to a form.
i tried getRecord function, but of no use 8-|
Please suggest how can i associate model to form or how can i get the values of form that is a model object which can be passed in POST request.

Thanks,
Ankit

Mark Wagoner
12 Sep 2014, 4:10 AM
If you set the form values using form.loadRecord() and pass it a model instance, you should get a model object back by calling form.getRecord()

AJAIN32
12 Sep 2014, 4:14 AM
Thanks for reply,

Ya i read that in sencha docs. do we have anything where we can cast/convert the form data into model.

The situation is like- i have a form opened i just want to fill that and add that to database, i want to pass the complete object as it is to Ajax Post request.

Any idea ??

Thanks,
Ankit

Mark Wagoner
12 Sep 2014, 4:16 AM
Initialize the form with an empty model instance, then use getRecord() to retrieve the model containing the updates values. You can then post this object to the server.

AJAIN32
12 Sep 2014, 4:20 AM
is there any configuration for that or i need to explicity write to set the form values on some render event ??
i tried to put the model in the form configuration but that doesn't worked .

Mark Wagoner
12 Sep 2014, 4:41 AM
Nothing special.

To initialize the form with an empty model instance:



var order = Ext.ModelManager.getModel('ParClient.model.Order');
this.getForm().loadRecord(order);


Then to get it back and save it:



var order = this.getForm().getRecord();
order.save({
scope: this,
callback: function (records, operation, success) {
...

AJAIN32
12 Sep 2014, 5:19 AM
Thanks a lot for such prompt reply,

i have tried the solution, I have just put this code to load a empty model record at Afterrender event.

and then at some Add button click i tried - userform.getRecord(), but still it shows undefined.

i know you mentioned very clearly, but still let me know if anything else is required to do ?

Thanks

AJAIN32
12 Sep 2014, 6:37 AM
Hey,

That was my mistake thats why it was coming undefined.

Just last thing i need to ask, while using getRecord() i am getting the blank record what i was setting at load time.

It will take the form values filled by user by getValues() only not by getRecord() ?

Any solution on that !!

Thanks

Mark Wagoner
12 Sep 2014, 7:17 AM
Sorry, I forgot you have to call form.updateRecord() to tell it to write the values back out.

AJAIN32
13 Sep 2014, 12:22 AM
Thanks !!

I tried that and now by "getRecord().data" i am getting the data available in form. as you said this is now in the form of Model, so it can be directly passed to the Ajax request.

but when i am trying to pass this in ajax request, at the controller action methode i am still getting null.

what i did -

var record = userForm.updateRecord()
record = userForm.getRecord()

Ext.Ajax.Request({

url : '/home/AddUser'
method :'POST',
params : {
userRecord : record.data
},
success : ...,
failure : ....
}
)

At Mvc Controller -

[HTTPPOST]
public bool AddUser(User userRecord)
{

}

I have tried encoding data in json and other things but nothing worked !

Any idea ??

Thanks,
Ankit

Mark Wagoner
15 Sep 2014, 4:02 AM
Have you verified that the POST is sending the correct data? The structure of the object on the client side must match the object on the server or it won't parse correctly.

basav
15 Sep 2014, 10:55 PM
Ankit, Try the following steps: 1. First of all check whether server code (.NET) is properly accepting the JSON REST request. For this, use REST Client Mozilla Add-On and send the request to the server. 2. If above one works properly, Check whether your client is properly sending the JSON data. For this, use Mozilla Firebug Add-on and under Net tab the request is proper. 3. If above one doesn't works, check whether you are properly encoding the data.

AJAIN32
16 Sep 2014, 3:45 AM
Thanks fo replying,

I found the request data in fiddler, its not mapping with each property.

if i include -

params{useRecord : user}

In Fiddler the request data is going like -
userRecord : "David",
userRecord : "Stephone",
userRecord : "9545654655"

so basically it is not getting converted in the model object to be passed.
Any idea about converting form data into sencha model so it can be passed directly.

Thanks,
Ankit

basav
17 Sep 2014, 1:30 AM
Can you post the expected data format from server side ?

You can use the Ext.JSON.encode for encoding into JSON.


var params = {};
params.para1 = 'value1';
param2.para2 = 'value2';

params: Ext.JSON.encode(params),
headers: {
'Content-Type': 'application/json'
}

AJAIN32
14 Oct 2014, 12:50 AM
Hi,

Expected Data format that i want to pass to MVC Controller is -

FirstName: "David",
LastName : "Stephone",
MobileNumber : "9545654655"

and as i mentioned earlier i am getting the request as -
userRecord : "David",
userRecord : "Stephone",
userRecord : "9545654655"

so basically its not getting mapped to object.

Any idea ??

Thanks,
Ankit