View Full Version : Ext.Ajax.request across context

12 Aug 2010, 12:41 AM
I have a problem with some ajax calls using Ext.Ajax.request.

I have an Ext page where I use the Ext.Ajax.request to make method calls and everything works fine, as long as I only make the calls within the context of the running application. The problem is that I have two different applications running on the same machine and in different contexts. If I make the same Ajax method call from context 1 to a method in context 2, then I do not get a correct answer returned.

My firebug reports that the "original" solution, where the call is made within the same context, is a "POST" while the second solution is of type "OPTIONS". The code is exactly the same for the two calls and I have to be able to make both kind of calls.

I hope somebody in here is able to help me on the right track for at solution. If you need further information about the code and the Ajax-call, please let me know.

12 Aug 2010, 4:34 AM
If the the domains do not match exactly, it will be a cross domain request, and cannot be made to work in a cross-browser manner.

If you are in control of the servers though, you could use JSONP. Which basically means creating a <script> tag. The server must then wrap the returned JSON data with a callback - see the ScriptTagProxy docs for a description of this.

I re-implemented Ext.data.Connection to enable this. But that was for 2.0. I still use that sometimes to access clients' JSON services (after getting them to set up the service to use the callback)


Another solution, if you are happy to not support IE, and you can modify how the servers set headers: You can use https://developer.mozilla.org/En/HTTP_Access_Control which will allow cross-domain XHRs and no <script> tags.

16 Aug 2010, 4:12 AM
Hi again,

It got it to work except that for some reason the requests are all GET now even though the method is POST when I debug the script. Is there something I need to set in the code, to get the correct method to be set?

I can not find the correct place in the code, where the method is added to the request, so I am a bit in the dark here :-)

16 Aug 2010, 4:24 AM
If went with using script tags, then they use GET and there's nothing you can do about it. You are basically just appending

<script src="/myServlet.do/param1=1&param2=2&..........">

to the header.

16 Aug 2010, 4:33 AM
I'm not using script tags (scriptTag: false) and the method is set to 'POST' in the Ext.Ajax.request. But if GET is the default and there is no way around it, I just have to find another way to do this :-)

16 Aug 2010, 4:39 AM
So you went with the FF/Chrome only solution of using the Access-Control headers?

AFAIK, you can use any methods. The server must reply with the methods it will allow. It takes some hacking to get it to work.

The info here, and the linked example pages show you how: https://developer.mozilla.org/En/HTTP_Access_Control

16 Aug 2010, 4:41 AM
This uses a POST: http://arunranga.com/examples/access-control/preflightInvocation.html

16 Aug 2010, 4:46 AM
By looking a bit closer to the code I can see the GET method is used, so that is correct. The problem seems to be in the code called on the other end of the request. I will have to figure that one out.

Thank you for your help so far.