0

I have a need to inject some data which is present in another page(url) on the current page load. Below is the code I am using.But it doesnt work, is there is a problem with below code and how can I debug this issue?

function loadHTML(url, storage)
{
    var xhr = createXHR();
    xhr.onreadystatechange=function()
    { 
        if(xhr.readyState == 4)
        {
            //if(xhr.status == 200)
            {
                storage.innerHTML = getBody(xhr.responseText);
            }
        } 
    }; 

    xhr.open("GET", url , true);
    xhr.send(null); 

} 


function loadWholePage(url)
{
    /**
        storage is the div id where I want to inject html
    */
    var y = document.getElementById("storage");
    loadHTML(url, y);
}   


window.onload = function()
{
   loadWholePage('link_to_the_page') ;
};

2 Answers 2

1

You hit a cross domain scripting problem. See Same Origin Policy.

As far as I know, JSONP can help you on this problem.

JSONP(JSON with Padding) provides a method to request data from a server in a different domain, something prohibited by typical web browsers because of the same origin policy.

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

Comments

0

There might be some issue with your function createXHR() and getBody(). Post the code you have written for those function. Moreover for debugging, you can put a console.log before calling getBody() and check what is responseText.

if(xhr.readyState == 4)
{
if(xhr.status == 200)
{
console.log(xhr.responseText);
storage.innerHTML = getBody(xhr.responseText);
}
} 

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.