Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ajax to access web from local file

Tags:

jquery

ajax

i'm a bit new to JQuery and ajax, so i apologize if this is a newbie's question.

I'm trying to use ajax from a local file to access the web (for example, getting a text file).
I'm not using IIS or anything, simple file from my hard drive (and i need it to stay that way).
Checked it on both IE8 and Chrome (version 11.0.696.60).

Here's some javascript to illustrate:

// use ajax to load from the web
$("#webText").click(function(){
    $.get("http://www.w3schools.com/jquery/demo_ajax_load.txt", function(result){
        alert(result);
});

This code is trying to load a text file from the web - the operation fails on both IE and chrome (won't get to get to the success function).
Chrome's notifies in the error console about "XmlHttpRequest cannot load _http://www.w3schools.com/jquery/demo_ajax_load.txt: Origin null is not allowed by Access-Control-Allow-Origin"

// use ajax to load from a local file
$("#localText").click(function(){
    $.get("demo_ajax_load.txt", function(result){
        alert(result);
});

This code is trying to load from a local text file.
IE: the operation succeeds.
Chrome: fails with same error as above.

At this point i thought it was impossible to communicate with the web from a local file, but then i came across a similar question: XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

Using the examples given there, I tried:

// use ajax to load json object from the web
$("#webJSON").click(function(){
    var url = 'http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&minx=-30&miny=0&maxx=0&maxy=150';
    $.get(url, function(json) {
        alert(json.photos[1].photoUrl);
    }, "jsonp");
});

And this code works great on both browsers. So obviously, it is possible to communicate with a web service from a local file.

Any ideas?

BTW - i'm more interested with the IE aspect of this, Chrome and other browsers are less of an issue.

Thanks.

like image 352
Bagelzone Ha'bonè Avatar asked May 09 '11 07:05

Bagelzone Ha'bonè


People also ask

Does AJAX work with local files?

Note that you cannot make an AJAX request to the local file system from an external domain in either browser - it would be a massive security flaw if you could. For this AJAX request to work in Chrome you need to make the request to a webserver.

Can AJAX work with web application?

Ajax stands for Asynchronous JavaScript and XML. In essence, Ajax is an efficient way for a web application to handle user interactions with a web page - a way that reduces the need to do a page refresh or full page reload for every user interaction.

Can JavaScript access local files?

JavaScript does not have direct access to the local files due to security and privacy. By using a file input and a File reader, you can read one or multiple local files. We can offer the user the possibility to select files via a “file input” element that we can then process.

Is AJAX better than fetch?

Fetch is compatible with all recent browsers including Edge, but not with Internet Explorer. Therefore, if you are looking for maximum compatibility, you will continue to use Ajax to update a web page. If you also want to interact with the server, the WebSocket object is also more appropriate than fetch.

What is the use of Ajax file?

AJAX is a misleading name. AJAX applications might use XML to transport data, but it is equally common to transport data as plain text or JSON text. AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes.

How do I use Ajax to communicate with a database?

AJAX can be used for interactive communication with a database. The following example will demonstrate how a web page can fetch information from a database with AJAX: Customer info will be listed here... When a user selects a customer in the dropdown list above, a function called "showCustomer ()" is executed.

How can I read the XML file using Ajax?

How can i read the xml file using AJAX? 1 Create a dynamic dropdown form that loads its data from a JSON file using JQuery 2 Avoiding "junk after document element" for jQuery .ajax with datatype: 'text'?

Why can't I read files from local machine using JavaScript?

That API "officially" allows your script to read files on the local machine. Javascript is work on client side but have limited access so it not able to access local files form the client machine. So you require to palce you content on server than you can use ajax and get the data in you div to display the client.


1 Answers

The problem is that you're running into the Same Origin Policy, which applies to all "real" ajax calls (calls actually using XMLHttpRequest).

The reason IE works but Firefox and Chrome don't is simple: IE doesn't apply the SOP when the origin is a local file and the resource you're trying to retrieve is on the web. Chrome and Firefox, on the other hand, apply the Cross-Origin Resource Sharing standard from the W3C and so include the relevant "this is my origin, will you let me talk to you?" headers — and w3schools is saying "No, I won't talk to you." ("null" is the "origin" value for the local machine.) The joy of having a choice of browsers is that they can make different design decisions on things like this.

The code you found that works isn't doing a real ajax call, it's doing JSON-P, which doesn't use XMLHttpRequest at all and so bypasses the SOP, but only for GET operations (not POST) and only if the other end supports it. (jQuery's get function can do both real ajax calls and JSON-P, the key to what it's doing is the dataType parameter, which in the example you showed is "jsonp".)

You may find this article useful. It describes using YQL (an HTML scraping service from Yahoo) as a cross-domain proxy, since YQL supports JSON-P.

like image 99
T.J. Crowder Avatar answered Oct 11 '22 03:10

T.J. Crowder