views:

100

answers:

4

Trying to use ajax, getJSON, and functions like that to fetch an external URL from a local (non-server) development computer. Is there a way to bypass the same origin policy, so that I can test locally, instead of having to upload to a server?

+1  A: 

There are different ways to get around this, depending on which browser you're using for development. For example:

  • In Firefox (Gecko), set security.fileuri.strict_origin_policy to false
  • In Chrome, start the browser with the option --allow-file-access-from-files

References: Firefox, Chrome

Ken Redler
A: 

Since this is a development issue and not a end-user/functionality issue, rather than focusing on getting AJAX to cross domains get your development environment set up as a proxy to fetch the most recent data from the production servers. This is actually really easy to do.

You'd need to set up a web server in your dev environment (if it doesn't have one already), and then configure the server to respond to 404 requests by fetching and then echoing production data. You can set up your server so that only the AJAX data files are picked up (otherwise, it will be confusing to debug other files if production assets start showing up on your development pages). So if http://dev.myserver.com/data/json/mydata.json is missing, your 404 script will get http://prod.myserver.com/data/json/mydata.json and echo it to the client. The nice thing about this set-up is that you can use mock data very easily: if the file is there in your dev environment, your AJAX script will get that; but if you then erase or rename that file, you'll get the production data instead. This feature has been so useful I can't recommend it enough.

If you're working with XML, I'd recommend duplicating the HTTP headers in the 404. If your 404 process responds with a Content-Type of text/html, you won't get any responseXML to parse.

Andrew
+2  A: 

We had the same need when developing our web app. Here's how we did it:

The browser and the server communicate only through JSON.
All the HTML is rendered in the browser using PURE (our JS template engine).
The browser code is developed locally like this:

We add a host parameter in the url of the app:

http://localhost/app.html?host=test.beebole-apps.com

In production, the JSON are sent to the server with a POST.
But here the function in charge of the ajax call will react to the host parameter and make a JSONP injection(GET) instead.

<script src="http://test.beebole-apps.com/?callback=f2309892&amp;json={...}" />
  • f2309892 is a temporary function, with a random name, that points to the method that will handle the response
  • json is the JSON we send to the server

It means you will need some cooperation from the backend to serve you the json wrapped in a callback function like:

f2309892( /*the json here*/ );

Except a size limitation(you can't send a big JSON to the server with a GET) it works like a breeze.
An other advantage is you can call all the different systems(development and test) from the same localhost.

Mic
this is great! i notice you don't have this local use of PURE in http://beebole.com/pure/documentation/what-is-pure-and-why/ ... Also - can you expand your words a slight bit to write a tutorial in http://beebole.com/pure/documentation/ ?
ina
also (just to be sure) - `http://localhost/app.html?host=test.beebole-apps.com` is on the clientside (where you have the client download an app with some sort of web server or URL paser in it...) and it calls the `test.beebole-apps.com` server?
ina
PURE is a JS library that build HTML from JSON data. It works on the browser. If you download the zip file, there is a directory called tutorial with some examples.
Mic
If you have a web server locally to develop, you can use http://localhost/app.html; If not, you can use something like file:///Users/mic/app.html or C:\test\app.html; And then in that page you inject the script tag with test.beebole-apps.com
Mic