0

I have a problem to get json. If I use: $.getJSON("file:///E:/WebProject/json/"+name+".json",resolve) It works!

But if I use: $.getJSON("/json/"+name+".json",resolve) It does not work. Why?

5
  • You don't have a running web server, right? Commented Jul 3, 2015 at 21:28
  • @Vohuman He's using Firefox, not Chrome, so that's fine, local Ajax requests are allowed. Commented Jul 3, 2015 at 21:28
  • Look at the actual request sent to the server. Does the resource path correctly match what is expected/hosted? What is the response? Commented Jul 3, 2015 at 21:30
  • @blex How do you know OP is using Firefox? The point wasn't Same-origin Policy. Commented Jul 3, 2015 at 21:36
  • @Vohuman Ok. Well, as you figured, Chrome and IE would block these requests, but not FF. And since his first call is working, I figured he was probably using FF, and that not having a server was not the issue. It's just the wrong path. Commented Jul 3, 2015 at 21:38

2 Answers 2

1

Try Changing

 $.getJSON("/json/"+name+".json",resolve) 

to

 $.getJSON("json/"+name+".json",resolve) 

/json looks for the file in the root but I think json folder is in your web folder, not in the root.

Sign up to request clarification or add additional context in comments.

1 Comment

@JossefHarush take a look at it now.
0

It's because ...

When the first character is /, the browser fetch the file from the root of the site address

e.g.

file:///E:/WebProject + /json/name.json = file:///E:/json/name.json (maybe even without the E letter)

otherwise, the addition is relative:

file:///E:/WebProject + json/name.json = file:///E:/WebProject/json/name.json


  • as @Alp suggested, omit the first / and you should be fine
  • consider using a small local development server - if using python, cd to that folder and type python -m SimpleHTTPServer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.