0

I am trying to implement a simple functionality to read the filenames of the files in the posts directory.

My AJAX call is as follows(The URL is the relative URL of the posts directory)):

$.ajax({
    url: "posts/",
    success: function(data){
       console.log(data);
       $(data).find("a:contains(.html)").each(function(){
          console.log("Reading...");
          console.log($(this).attr("href"));
      });
   }
});

I get a 404 Not Found error with the following:

n.ajaxTransport.k.cors.a.crossDomain.send   @   jquery.js:8630
n.extend.ajax   @   jquery.js:8166
(anonymous function)    @   index.js:3
n.Callbacks.j   @   jquery.js:3099
n.Callbacks.k.fireWith  @   jquery.js:3211
n.extend.ready  @   jquery.js:3417
I   @   jquery.js:3433

First of all, why am I getting cross domain error when this is in the same domain?

How should I get around resolving it?

Thanks.

dshgna
  • 812
  • 1
  • 15
  • 34
  • I've already Googled and I posted here because I couldn't understand how to resolve it. – dshgna Sep 08 '15 at 07:54
  • 1
    404 not found means it could not find the URL. Check your developer tools in the browser and look at the Net tab to see what URL it is trying to access. – mccannf Sep 08 '15 at 08:08
  • 1
    You're sure your path to `posts/` is correct? Try using an absolute URL just to be sure. I've seen instances of the Chrome dev tools throwing CORS errors when in actual reality it was because the XHR was pointing to a nonexistant file. – Scott Sep 08 '15 at 08:09
  • @Scottie: Unfortunately, changing the URL returns the same error. – dshgna Sep 08 '15 at 10:06
  • @mccannf: yes. Even when using relative, the URL points to the correct path. – dshgna Sep 08 '15 at 10:06
  • And to confirm - when you copy the URL that is displayed in the Net tab and paste it into the browser bar, the correct page comes up and no 404 errors are displayed? – mccannf Sep 08 '15 at 10:43
  • Sorry for the confusion: posts here is a directory, not a file. – dshgna Sep 08 '15 at 10:59
  • Aha. You need to use `file:///` instead of http. And look out for this: http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-allow-origin-for-file?lq=1 – mccannf Sep 08 '15 at 11:05
  • Isn't file:/// supposed to be only for local development? – dshgna Sep 08 '15 at 11:08
  • Yep. Which is what you seem to be asking for. If not, you need to create a webpage that will do a directory listing for you. – mccannf Sep 08 '15 at 11:11

0 Answers0