0

Because my title is too short, I will explain more clearer. I have create a code in JavaScript . And I have two options to run :

1) Run on machine : simple click into html file.

2) Run on local server : mean I start Apache, and start this html file in localhost.

( http://localhost:85/Javascript/index.html for example)

When I choose solution 1, no thing happen. And when I choose solution 2, happen as I wish. But I don't know why.

Here is my code. Purpose : take a json file and process it.

<script>
        window.onload = function(){
            var url = "http://localhost:85/javascript/json1.json";  // problem here
            var request = new XMLHttpRequest();
            request.open("GET", url);
            request.onload = function(){
                if (request.status == 200){
                    update(request.responseText);
                }
            }
            request.send(null);
        };
function update(responseText){ // some code here }
</script>
1

2 Answers 2

3

You cannot use AJAX to read content from a different domain.

Javascript running from file://whatever cannot read localhost:85.

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

Comments

2

Did you replace this line with the server's original path?

var url = "http://localhost:85/javascript/json1.json";

With

var url = "http://10.0.0.X:85/javascript/json1.json"; // Did you change the right path?

And make sure, the page is not called with the file:// protocol!

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.