0

I uploaded my page to a server and when I'm trying to take photos from a folder it gives an 403 (Forbidden) error. When I tried it on a local host it worked.

var dir = "color/real";
var fileextension = "230x128.jpg"
$.ajax({
        url: dir,
      success: function(data){
        alert("succes");
    $(data).find("a:contains(" + fileextension + ")").each(function () {
            var filename = this.href.replace(window.location, "").replace("http:///", "");
            var name = this.href.replace(window.location, "").replace("http:///", "").replace(/\_/g, " ").replace(/230x128\.jpg/g, "");
            var custom = '<div class="thumbnail color b' + alpha[classIndex] + '" style="background-image:url(' + dir + filename + ')"><div><p>' + name + '</p></div></div>';
            $("#parent").append(custom);
        });
      } 
    });

});

Andrea
  • 11,801
  • 17
  • 65
  • 72
Tomas
  • 33
  • 8
  • Is it your server? Inspect the logs (in Apache, typically `error_log`), they should tell you why the server refused to serve the content. Not your server? Ask the owners why their server is refusing to serve you. Can you access the content directly from your browser by putting the URL in the browser address bar? – Amadan Jul 28 '15 at 09:45
  • Common reasons for 403 are: **No index page** , **Permissions and ownership errors** ; do check whether you can access the php page directly or not. – Maz Jul 28 '15 at 09:46
  • @Amadan i can not access the content folder directly, same error. – Tomas Jul 28 '15 at 09:49
  • @Maz i changed permissions to 755 for folders and 644 for files and there is one index.htm file – Tomas Jul 28 '15 at 09:49
  • 1
    When you use ajax to fetch files from another server (cross-domain), the remote server must be configured to allow requests from your domain by sending back relevant headers. – Shahar Jul 28 '15 at 09:50
  • @Shahar Not from another server, just from another folder. About what headers are you talking? – Tomas Jul 28 '15 at 09:51
  • @Tomas if you can't access the file directly from browser, your script will not be able to either. Also, your content directory does it have any index file ? For example: if your directory doesn't have any index file but you try to browse it; it will give you **403** error. Instead you should directly call the file. i.e: `var dir = "color/something.jpg";` – Maz Jul 28 '15 at 09:55
  • Looks like you are trying to do an ajax call to the images directory - you need to call a page or file – Pete Jul 28 '15 at 09:55
  • @Tomas I'm referring to CORS headers in the response. See this flowchart: http://www.html5rocks.com/static/images/cors_server_flowchart.png Do you see any other messages in your browser console? – Shahar Jul 28 '15 at 09:56
  • @Maz I have no index files in directory where the images are. What i am trying to do here is like automated image search, which takes all images from the folder ad displays it to a web page. I am calling color/real folder and searching for all images with 230x128.jpg endings – Tomas Jul 28 '15 at 10:00
  • @Shahar `XMLHttpRequest cannot load http://www.grindudangos.lt/Marmoleumas/Marmoleumo%20produktai/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://grindudangos.lt' is therefore not allowed access. The response had HTTP status code 403.` – Tomas Jul 28 '15 at 10:02
  • I am not sure if this is the best way to do it, but since it is working in your local I will not talk about that. `No 'Access-Control-Allow-Origin' header is present on the requested resource` is probably your clue as @Shahar suggested. – Maz Jul 28 '15 at 10:06
  • @Maz Maybe you could suggest me how should i solve the problem with 'Access-Control-Allow-Origin' – Tomas Jul 28 '15 at 11:23
  • @Maz do you have some suggestions what would be the better way to take pictures from a folder and display them on the web page? – Tomas Jul 28 '15 at 12:26
  • There is something weird happening that I can't see in your code. The error message is quite clear: you are on `http://grindudangos.lt`, but are trying to request from `http://www.grindudangos.lt`. Find out where the `www.` comes from and kill it with fire, if they are indeed the same thing. If not, then you need ACAO headers to allow the other domain. On Apache, you can do it [like this](http://stackoverflow.com/a/11691776/240443); you'd have `Header add Access-Control-Allow-Origin "http://grindudangos.lt"` – Amadan Jul 29 '15 at 01:24
  • @Amadan Thank you! It is working, the problem was a header. – Tomas Jul 29 '15 at 01:57

0 Answers0