PDA

View Full Version : Add parameters to Ajax proxy



kortovos
24 Sep 2010, 6:48 AM
How do you add parameters to an Ajax store?

I am bypassing the cross domain policy by using a php proxy on my server. I am trying to send a few needed parameters to the php page and I expect the response to be xml.

But how do I send certain parameters to the php page on the server?



var orderStore = new Ext.data.Store({
model: 'order',
url: varxml,
proxy: {
type:'ajax',
id: 'data',
url: 'proxytest.php',
method: "POST",
params:{action: 'askorderlist',
question: 'uborderlist',
value: '2010-08-01',
user: 'username',
password: 'password'
},
reader: {
type: 'xml',
record: 'order'
}
},
autoload: true,
});

steve1964
25 Sep 2010, 5:13 AM
Just append the parameter to the url:


var orderStore = new Ext.data.Store({
model: 'order',
url: varxml,
proxy: {
type:'ajax',
id: 'data',
url: 'proxytest.php?username=dummy&age=' + somevar,
method: "POST",
params:{action: 'askorderlist',
question: 'uborderlist',
value: '2010-08-01',
user: 'username',
password: 'password'
},
reader: {
type: 'xml',
record: 'order'
}
},
autoload: true,
});

kortovos
26 Sep 2010, 10:05 PM
Isn't that a 'GET'? And if so, isn't there a limit to the number of characters you can append to the url?

What if I want to use a 'POST'?

evant
26 Sep 2010, 10:25 PM
Ext.setup({
onReady: function(){
Ext.regModel('Foo', {
fields: ['a']
});

new Ext.data.Store({
autoLoad: true,
model: 'Foo',
proxy: {
url: 'data.asp',
type: 'ajax',
extraParams: {
foo: 'bar'
}
}
});
}
});


By default all read operations will be a get. You can override it for a single instance by using:



// create store
myStore.proxy.actionMethods.read = 'POST';
myStore.load();


Or globally:


Ext.apply(Ext.data.AjaxProxy.prototype.actionMethods, {
read: 'POST'
});
// create store

kortovos
26 Sep 2010, 10:36 PM
Thanks Evant for your quick response. In your example the extraParams you pass to the proxy also are present in the model, is this a necessity?

I am still getting errors. Is there a 'success' and 'failure' parameter for stores or readers, like with an Ext.Ajax.request?

XHR finished loading: "http://localhost/proxytest.php?_dc=1285569086510".ext-touch-debug.js:7732
Uncaught #<an Object>
getResponseData ext-touch-debug.js:7732
readext-touch-debug.js:7235
(anonymous function)ext-touch-debug.js:6620
onCompleteext-touch-debug.js:14577
onStateChangeext-touch-debug.js:14524
(anonymous function)ext-touch-debug.js:2618

evant
26 Sep 2010, 10:38 PM
No, I just happened to use the same name for the model and the property, they are in no way related.

As for the other one, it sounds like you're sending back malformed data.

kortovos
26 Sep 2010, 10:41 PM
The response should be something like : <?xml version="1.0" encoding="UTF-8"?><orderlist><order><orderid>2005048</orderid></order><order><orderid>3000359</orderid></order><order><orderid>2007062</orderid></order><order><orderid>2007118</orderid></order><order><orderid>3000362</orderid></order><order><orderid>2007134</orderid></order><order><orderid>2007135</orderid></order><order><orderid>2007142</orderid></order><order><orderid>3000367</orderid></order><order><orderid>2007178</orderid></order></orderlist>

Could it be the header (<?xml version="1.0" encoding="UTF-8"?>) that is causing the problem?
With Chrome dev tools, when I click on "http://localhost/proxytest.php?_dc=1285569086510", it displays the correct output.

kortovos
26 Sep 2010, 11:11 PM
And my model is

Ext.regModel('order', {
fields : [ {
name : 'orderid',
type : 'integer'
}]
});

Anyone an idea what I am doing wrong?

kortovos
27 Sep 2010, 12:37 AM
Oke, I made sure that the server returns text/xml, by adding 'header("Content-Type: text/xml");' to the php document. So now I don't get any errors, but the store still doesn't get filled.

steve1964
27 Sep 2010, 12:52 AM
How you check if the store is filled?

kortovos
27 Sep 2010, 1:02 AM
Oke, I made sure that the server returns text/xml, by adding 'header("Content-Type: text/xml");' to the php document. So now I don't get any errors, but the store still doesn't get filled.

Nevermind, I was a bit to enthousiastic with my function. I didn't wait for the response before counting the records in the store.
It functions fine.

kortovos
27 Sep 2010, 1:07 AM
How you check if the store is filled?
With a getCount(), but I didn't wait for the response. It is working fine now.

kortovos
27 Sep 2010, 2:10 AM
Another question. Is it possible to instruct the app to wait until the store is finished with loading? I have tried with a listener (datachanged) but for some reason it won't fire after the store has completed its read.

kortovos
27 Sep 2010, 2:13 AM
Another question. Is it possible to instruct the app to wait until the store is finished with loading? I have tried with a listener (datachanged) but for some reason it won't fire after the store has completed its read.

Nevermind, already found the problem

ickyfoot
15 Jan 2011, 8:44 PM
This is my first post on the Sencha forums. Let me start by saying, Sencha Touch is amazing! It's my first time using ExtJs, and while there's a bit of a learning curve, it really is incredible once you start to understand it.

Anyway, the reason I am posting in this thread is because I'm trying desperately to use a mysql-populated Store to generate the items in a form's selectfield. I can make this work if I use inline Store data, but just can't seem to populate the Store from a php file. Here is the client-side code:


Ext.apply(Ext.data.AjaxProxy.prototype.actionMethods, {
read: 'POST'
});

Ext.regModel('listName', {
fields: [
{name: 'name', type: 'string'}
]
});

var store = new Ext.data.Store({
model: 'listName',
proxy: {
type: 'ajax',
method: 'post',
url : 'serverside/listContent.php',
params: {action: 'gtLstNms'},
reader: {
type: 'json',
root: ''
}
},
autoLoad: true
});

And, the php:


$result = mysql_query("SELECT name FROM table");
while($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo json_encode($arr);

As you can see, this is quite simple from a data/content perspective.

Anyway, when I access the form and click on the select field I get "cannot read property 'id' of undefined.'

Edit: Oh yeah, when I use a simple Ext.Ajax.request and console.log the the response from the same php content as above, I get [{"name":"Test"}], which is what I'd expect (just one entry in the db for now). Unfortunately, I'm not aware of how to check what, if anything, I'm getting back from the Store's ajax proxy.

Any help is much appreciated. My apologies for resurrecting this old-ish thread and/or hijacking it, but this seems moderately related, and not worth its own space.

Many thanks!

-Eric

ickyfoot
15 Jan 2011, 9:22 PM
Heh, I'm realizing the problem may simply be that the server hasn't populated the Store before the form finishes rendering. Now I have to figure out how to handle that issue. Hopefully I haven't just wasted space here, but it looks like maybe I have ;).

ickyfoot
15 Jan 2011, 9:26 PM
Ugh! It was simply because I was using "params" instead of "extraParams" in my proxy! Sincere apologies for cluttering everything up and bumping such an old thread over such a simple issue.

codehtmai
24 Apr 2012, 1:05 PM
Ugh! It was simply because I was using "params" instead of "extraParams" in my proxy! Sincere apologies for cluttering everything up and bumping such an old thread over such a simple issue.

Ickyfoot, you saved the day. extraParams did it for me, I just couldn't find where to set that.