PDA

View Full Version : Can't get ExtJS REST proxy to POST



cbroughton
13 Dec 2011, 1:37 PM
I'm using this ExtJS REST proxy to get and post data to my REST back end





this.eventStore =Ext.create('Extensible.calendar.data.EventStore',{
autoLoad:true,
proxy:{
type:'rest',
url:'http://www.notmyrealdomain/testapp/index.php/api/events',
format:'json',
noCache:false,

reader:{
type:'json',
root:'data'
},

writer:{
type:'json',
nameProperty:'mapping'
}
}
}

When I do a print_r on the PHP receiving end it looks like $_POST is empty



print_r($_POST)
array()



When I look at Firebug I see that my API page does have JSON being posted to it but the response is just the empty $_POST array.I have checked my config.php BASE_URL and it is my full url http://www.notmyrealdomain.com/testappand I'm pretty sure my proxy above is posting to that url, it's not returning 404 and it doesn't look like it would be cross domain or anything.Does anyone have any suggestions? I've been looking at this for 8 hours now and need some new eyes.

dogomatic
14 Dec 2011, 1:14 AM
It's in the post body as json, not in the post query args.

http://stackoverflow.com/questions/6702490/how-to-use-extjs4-save-in-php-server/6703326#6703326

use:


$form_data = json_decode(file_get_contents('php://input'));


also, don't forget to use the code tag (like html with square brackets) when posting code.

tvanzoelen
14 Dec 2011, 1:46 AM
Are you sure your proxy is posting and not getting?

In the case it is getting. Set actionMethods on your proxy

like


actionMethods: {create: "POST", read: "POST", update: "PUT", destroy: "DELETE"}


in the default setting read is a GET.

cbroughton
14 Dec 2011, 8:24 AM
It's in the post body as json, not in the post query args.

http://stackoverflow.com/questions/6702490/how-to-use-extjs4-save-in-php-server/6703326#6703326

use:


$form_data = json_decode(file_get_contents('php://input'));


also, don't forget to use the code tag (like html with square brackets) when posting code.

That's it!
That explains why firebug didn't look quite right.
Thank you.

Is that normal behaviour in REST APIs? And is it pointed out in the Sencha docs? I don't recall seeing anything like that.

dogomatic
14 Dec 2011, 5:30 PM
Nice!

It is normal if you're posting json. Query args have to be of the form key=value, so how would you represent a json object with nested objects and arrays? It's hacky but possible to do with some sort of dot notation, but much more sensible to use the post body.

I don't know if it's in the docs, but I've seen this question asked a few times.

Maybe make a post in the doc bugs thread:
http://www.sencha.com/forum/showthread.php?135037-API-Documentation-Content-Bugs

Also, it's partly php's fault for making the post body so obscure.

gbs1230
15 Nov 2012, 5:30 AM
Thanks , helpful


Are you sure your proxy is posting and not getting?

In the case it is getting. Set actionMethods on your proxy

like


actionMethods: {create: "POST", read: "POST", update: "PUT", destroy: "DELETE"}


in the default setting read is a GET.