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

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.

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

Thanks
Layman

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.