PDA

View Full Version : Cannot access data submitted by doRequest



phamtranquocviet
28 Apr 2012, 3:54 PM
I have a grid of users. At the bottom is a "Add User" button to add user. When clicked, a form pops up to enter user info (first, last name), and clicking "Add" inside the form submits info to server for insertion into the database. On the server, I dump out the request (var_export($_REQUEST, 1)) but the first and last names are no where to be found.

Here is my controller code:


Ext.define("Iip.controller.iip.adm.AddUsers", {
extend: "Ext.app.Controller",

handlePostAddUser: function(operation) {
var response, store;

if(operation.success == false) {
Ext.get("add-user-error").update("Adding failed");
return;
}

response = JSON.parse(operation.response.responseText);
store = Ext.StoreManager.lookup("user-store");
store.add(response.record);
this.destroy();
},

handleAddUser: function(addButton) {
var window, form, store, operation;

window = addButton.up("window");
form = window.down("form").getForm();
if(!form.isValid()) {
return;
}

store = Ext.StoreManager.lookup("user-store");
var operation = Ext.create("Ext.data.Operation", {
action: "create",
records: [store.createModel(form.getValues())]
});
console.log(operation);
store.getProxy().doRequest(operation, this.handlePostAddUser, window);
},

init: function() {
this.control({
"add-user-form #add-user": {
click: this.handleAddUser
}
});
}
});


Here is what Firebug shows when the form is submitted to server:
POST http://vm/~phamtranquocviet/extjs/cadao/index/adm_index.php?c=add_user

JSON
email "qnguyen@gmail.com"
firstName "Quyen"
lastName "Nguyen"
userId 0

Source {"userId":0,"firstName":"Quyen","lastName":"Nguyen","email":"qnguyen@gmail.com"}

What am I doing wrong?

I notice Firebug output shows that the posted data does not have a name (e.g. data => json_object). Only json_object. This is probably why data is not actually submitted, but I don't know how to fix it.

Thanks for your help.

vietits
28 Apr 2012, 5:05 PM
To send data in the form of {"data": json_object}, you should config root for writer of proxy


proxy: {
...
writer: {
type: 'json',
root: 'data'
}
}

phamtranquocviet
28 Apr 2012, 9:26 PM
Firebug now shows the following output, but var_export($_REQUEST, 1) still shows no first name and
last name.

data Object { userId=0, firstName="Quyen", lastName="Nguyen", more...}

email "qnguyen@gmail.com"

firstName "Quyen"

lastName "Nguyen"

userId 0

Source
{"data":{"userId":0,"firstName":"Quyen","lastName":"Nguyen","email":"qnguyen@gmail.com"}}

I guess it still does not work because data is still inside an object. It's still not like:

"data":{"userId":0,"firstName":"Quyen","lastName":"Nguyen","email":"qnguyen@gmail.com"}

How do I fix it so that it submit like that?

vietits
29 Apr 2012, 5:43 AM
<proxy>.doRequest() send records as an object or an array of objects. If you want to use <proxy>.doRequest() to send form fields as <form>.submit() does, then let use params instead of records when creating operation object.


handleAddUser: function(addButton) {
var window, form, store, operation;

window = addButton.up("window");
form = window.down("form").getForm();
if(!form.isValid()) {
return;
}

store = Ext.StoreManager.lookup("user-store");
var operation = Ext.create("Ext.data.Operation", {
action: "create",
//records: [store.createModel(form.getValues())]
params: form.getValues()
});
console.log(operation);
store.getProxy().doRequest(operation, this.handlePostAddUser, window);
},

phamtranquocviet
29 Apr 2012, 6:58 AM
Thanks vietits. Will give params a try.

Just curious. When you send data as records or array of records. How do you access on the server?

thanks again.

vietits
29 Apr 2012, 5:09 PM
You should have a look at $HTTP_RAW_POST_DATA.