问题
I'm writing an application in Sencha Touch 2, and as part of it I have a number of stores that use ajax proxy and json reader to load automatically from my external web service. The service is hosted on a different domain than the client, so I had to set Access-Control-Allow-Origin on the server to make this work.
Everything works swimmingly with this setup, but I was using Fiddler to look at request performance and I noticed that every call of my webservice actually goes out twice. Once as an OPTIONS request, and then again as the expected GET. As far as I can tell the OPTIONS request serves no useful purpose -- at least in my setup -- so I'd like to get rid of it to make loading performance snappier.
Does anyone know what's going on with this? It'd be great if there was a config I could set on the proxy that would turn it off, but I haven't found on e yet.
Thanks for looking!
回答1:
I figured this out, it all had to do with the x-requested-with header that Sencha passes along with its ajax calls. This blog post (http://remysharp.com/2011/04/21/getting-cors-working/) goes into the details, but the short version is that in a weird piece of functionality, the browsers send out that pre-flight OPTIONS check only if you are including any custom headers with the request. If you don't add any headers to the request and just leave it plain, the GET will go through just fine with no OPTIONS check.
I dug in the source and found that Sencha actually does have a config that controls whether to send x-requested-with along or not, it's just not very exposed in normal usage.
Ext.Ajax.setUseDefaultXhrHeader(false);
I just set that before the requests go out, it drops the x-requested-with, and only the GET goes out.
回答2:
You need the OPTIONS request because you are accessing the data from different domain. The options call it's used to get the value of Access-Control-Allow-Origin parameter. The cross domain wouldn't work if an OPTIONS request isn't made.
来源:https://stackoverflow.com/questions/10236056/when-loading-a-store-in-sencha-touch-2-how-can-i-stop-the-additional-options-ht