AJAX code没有在本地运行AJAX、code

2023-09-10 14:56:27 作者:起什么群名字好听

code读取子文件夹下面给出

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;
}

这code运行完美,当我用它在本地主机。但是它在本地运行(从文件位置IE)时不运行。我有12个子文件夹。所以,当我使用localhost我得到12的输出,但是当在本地运行我只得到了0输出。

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.

会是什么问题?请帮我.. 我是新的使用jQuery。所以,如果这是我的错,请通知吧。 在code我只使用HTML,jQuery的,JS,但不是PHP的。

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.

推荐答案

这是因为浏览器跨域策略。您不能发送发出请求已发送外域AJAX请求。所以基本上,你不能用ajax在本地都没有。

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.