1

Code to read subfolders is given below

function countFolder(){
    var dir='albums';
    var count=0;
    alert(dir);
    $.ajax({
        url: dir,
        async:false,
        success: function (data) {
            $(data).find("a:contains(" + 'album' + ")").each(function () {// function to read foldera name contains 'album'
                count++;
                //alert(url);
            });
        }
    });
    return count;
}

This code runs perfectly when I use it on localhost. But it does not run when run locally(i.e. from file location) . I have 12 sub-folders. So when I use localhost I get the output of 12, however when run locally I only get the output of 0.

What would be the problem? Please help me.. I am new with jQuery. So if it is my mistake please notify it. In code I only use html, jQuery, js, but not php.

Daedalus
  • 7,586
  • 5
  • 36
  • 61
aju2529
  • 65
  • 7

2 Answers2

3

This is because of Browsers cross-domain policy. You can't send ajax request outside the domain from which the request have been send. So basically, you can't use ajax locally at all.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
  • And it varies by browser. Chrome disallows ajax using the `file://` protocol, but some other browsers allow it. – T.J. Crowder Sep 25 '13 at 06:59
  • Sorry I did n't get you will you explain it. – aju2529 Sep 25 '13 at 06:59
  • 1
    @user2812110: It's because of the [Same Origin Policy](http://en.wikipedia.org/wiki/Same_origin_policy). Some browsers (like Chrome and apparently what you're using) disallow all ajax calls with `file://` URLs, considering those violations of the SOP. (If you look in the browser console, you'll probably see a message saying that a call to "origin null" was disallowed.) The answer is to use a server (like you were with localhost). – T.J. Crowder Sep 25 '13 at 07:01
  • 1
    When you run Ajax request on domain: `domain.com`, the only addresses you can request are `http://domain.com/something` - the ones that are in same domain. Subdomain is not the same domain. And when operating on local file, It can't find origin domain, sets it to NULL, as there is no origin domain, it can't allow every unknown origin, because this would be huge security risk. – Flash Thunder Sep 25 '13 at 07:06
  • I think in localhost it is http:// and in localfile it is file:// Is it right?? is it the problem? – aju2529 Sep 25 '13 at 07:17
  • How can change this SOP problem – aju2529 Sep 25 '13 at 07:22
  • Run a local webserver. Use for example [XAMPP](http://www.apachefriends.org/en/xampp.html). – Flash Thunder Sep 25 '13 at 07:52
  • You can try to run Chrome with param: `--disable-web-security` – Flash Thunder Sep 25 '13 at 08:46
  • @Flash Thunder:No output same as previous. – aju2529 Sep 25 '13 at 09:04
  • So maybe you simply can't override this. – Flash Thunder Sep 25 '13 at 09:10
-2

Ajax makes a call to the server. When you open from localhost, the page opens with help of a server (xampp or tomcat). But when you open the page from the file location, it just displays the static content, only html and js, but not any server side code. Even php code will not work if you open from file location