Enabling CORS on Chome, IE9, Safari & Firefox

by Steven 3. October 2011 16:17

In my previous post i discussed how one can add CORS request headers to Firefox via an extension, which is very useful working with PhoneGap deployed apps which you can debug locally in the browser under the file:/// protocol.

My preferred browser is Chrome and although it was great I could do this with Firefox i was pretty determined to find a way to do this with Chrome. The following is the solution using Fiddler and a bit of script hacking.

 

1. Get Fiddler.

2. Go to the Filters tab and click Use Filters.

3. Add the domain you are working with to the hosts box (optional but stops a ton of traffic appearing).

4. Click the FiddlerScript tab.

5. Go to the OnBeforeRequest function and add the following:

if (oSession.HostnameIs("YOUR_DOMAIN_OR_IP_ADDRESS")) {

oSession.oRequest["Access-Control-Allow-Origin"] = "*";

oSession.oRequest["Access-Control-Allow-Methods"] = "POST,GET,PUT,DELETE";
}

6. Go to the OnBeforeResponse function and add the following:

if (oSession.HostnameIs("YOUR_DOMAIN_OR_IP_ADDRESS")) {
oSession.oResponse["Access-Control-Allow-Origin"] = "*";
oSession.oResponse["Access-Control-Allow-Methods"] = "POST,GET,PUT,DELETE";
}

7. Click the Save Script button.

8. Run your requests in Chrome :-)

 

 

Tags: , ,

tech