0

Hi there,
here is the code...

 <script type="text/javascript">
    function ajax_json(){

    var hr = new XMLHttpRequest();

    var url = "news.json";
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/json", true);
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var data = JSON.parse(hr.responseText);
            var results = document.getElementById("results");
            //results.innerHTML = data.news2.head +" <br/>Full report: " +data.news2.detail;
        results.innerHTML = "";
        for(var obj in data){
                results.innerHTML += "<strong>"+data[obj].head+"</strong>"+"<br/>" +data[obj].detail+"<br/>";
        }
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(null); // Actually execute the request
    document.getElementById("results").innerHTML = "requesting...";
}
    </script>

The above code displays data from json file when url is like this var url = "news.json"; but when I change the Url to
http://mydomain.cpm/news.json it does not display the data. And I am running this file from localhost.
So what is happenning here?

Any help? thanks in advance

2
Contributors
3
Replies
16
Views
4 Years
Discussion Span
Last Post by TomH.PG
0

You need to add localhost as an authorized origin - this would be CORS in javascript terminology - Cross-Origin Resource Sharing. You will need to add http headers in the response to indicate to the client browser that requests originating from code loaded on other domains can make http requests via javascript to your target/remote site.

0

@tomh thanks for the reply but no idea what you're talking about.

Thanks
Layman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.