View Full Version : encodeParams option for Connection class
I was wondering if it's possible to optionally encode params on ajax requests. When using Ext.lib.Ajax.request, it can easily be done manually. However, when using the Connection object you can't really do it manually if you use baseParams and params, as Ext will try to combine them before sending the request. If they are already encoded, this probably won't work.
Here is an example of how I would like it to work:
var conn = new Ext.data.Connection({
url: 'myUrl',
baseParams: myBaseParams,
encodeParams: true
});
conn.request({
params: myParams
});
If encodeParams is true and it's a post, the connection request method will automatically encode the combined baseParams and params before the Ajax.lib.request.
I am requesting this as AjaxPro expects the params to be encoded. If this feature is added (and the request header feature which has been approved for a future release is added), there will be a big advantage for AjaxPro users, as we will no longer need to use custom connections and/or proxies when using AjaxPro.
I have already added an Ext provider in AjaxPro and have sent the code to Michael (the developer of AjaxPro). If this change is made, I will update the code immediately. After speaking to a few people who use AjaxPro and Ext, I think they will be very happy if this can be done.
Thank you again for always seriously listening to our feature requests. If you need further information, please let me know.
jack.slocum
4 May 2007, 8:22 AM
So you want them encoded before they go to the request() call? I'm not sure what exactly this does.
Thanks for the response.
I took another look at the Connection class and I overlooked something. This is the code at the top of the Ext.data.Connection.request method:
var p = options.params;
if(typeof p == "object"){
p = Ext.urlEncode(Ext.apply(options.params, this.extraParams));
}
If params is an object, it will combine the params and extraParams and urlEncode them. This is where the AjaxPro method is having a problem. Let me explain with an example.
I have an AjaxPro method that expects one int parameter named userID. It expects the post to be :
{"userID":1}
The following will not work:
var conn = new Ext.data.Connection();
conn.request({
url: 'myUrl',
params: {userID: 1}
});
The reason this will not work is the post will be:
userID=1instead of what I have listed above.
If I change the code to the following it will work:
var conn = new Ext.data.Connection();
conn.request({
url: 'myUrl',
params: Ext.encode({userID: 1})
});
The reason it works is that it's a string, so it bypasses the code that combines the params and the extraParams and urlEncodes it. However, if I do something like this, it won't work:
var conn = new Ext.data.Connection({
extraParams: Ext.encode({anotherParam: 2})
});
conn.request({
url: 'myUrl',
params: Ext.encode({userID: 1})
});
The reason this won't work is because the code to combine the extraParams and the params is only called if params is an object, which it is not in this case.
Having an option to JSON encode on posts rather than urlEncode would help. Here's an example of how the request method could be changed to allow this (untested):
var p = options.params;
if (typeof p == "object") {
p = Ext.apply(options.params, this.extraParams);
if (options.jsonEncodeParams || (typeof options.jsonEncodeParams == "undefined" && this.jsonEncodeParams)) {
p = Ext.encode(p);
} else {
p = Ext.urlEncode(p);
}
}
By doing this, the following code would work:
var conn = new Ext.data.Connection({
extraParams: {anotherParam: 2},
jsonEncodeParams: true
});
conn.request({
url: 'myUrl',
params: {userID: 1}
});
I hope this helps clear things up. Sorry about my original post.
I quickly tested the above code change, and it works as expected. This change to Ext would indeed resolve the problem when using AjaxPro.
Note: If jsonEncodeParams is not passed or is false, it will continue to work the same way it did before.
jack.slocum
4 May 2007, 9:46 AM
It seems non-standard. Might a suggest something like this:
Ext.data.Connection.prototype.request =
Ext.data.Connection.prototype.request.createInterceptor(function(o){
if(/*do some sanity checks*/){
o.params = Ext.encode(Ext.apply(o.params||{}, this.extraParams));
}
});
This would accomplish what you want globally.
Hi Jack. I appreciate the suggestion, and I personally have no problem doing this. It appears that this will work just fine. I also sent Michael an email earlier this week asking for his opinion on this as well.
Off topic, I will be overriding the Ext.lib.Ajax.request in my code so that I can start using request headers. I realize that you're very busy and I don't want to push you on adding this feature. However, I would like my code to have as few changes as possible, so it would be helpful to know how we might set this info with the Connection class when it is available. For example, do you think we will be able to set the headers using something like this?
var conn = new Ext.data.Connection({
...
//headers or requestHeaders? (I like headers because it's shorter and I think it can be implied that it is request headers).
//can accept an array of headers objects
headers: [
{header1: myHeader1},
{header2: myHeader2}
]
//OR
//and can accept one header object
headers: {header1: myHeader1}
});
I realize you can't guarantee how you will implement it, I just want to reduce the chance that I will need to change my code. Does this look about right?
The reason I'm asking here is that it's somewhat related. I would like to change the Ext provider in AjaxPro so that it will be able to use the standard connection and proxy classes in Ext, rather than custom ones.
Hi Jack,
Thanks again for the suggestion. You can ignore my off topic questions regarding the future response header implementation.
For those interested, I was able to get AjaxPro and Ext working together without the need of a custom connection and proxy:
http://extjs.com/forum/showthread.php?p=28287#post28287
jack.slocum
4 May 2007, 11:51 AM
I still have to do more research on the best way to do cross-lib header setting. The other problem is if I add what you suggest above, I have to add it throughout the lib. I don't look forward to that and may try an alternative.
No worries Jack. I think we were posting at the same time. Please see my previous post. :)
For AjaxPro, I no longer need the use of request headers, as the info can be passed through the query string now. With the use of the interceptor, it is quite easy to do what I need to do. Thanks again!
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.