简体   繁体   中英

Ajax Pull Content From another webpage

So I got this code for pulling rss feeds from another website (i asked them, and they gave me permission) I Don't know what should i Write in TAG1 and TAG2. Basically that is just my problem: Here is the html (its an ajaxed page)

<!doctype html>
<html lang="hu">
<head>
    <title>Videók</title>
    <meta charset="utf-8"/>
        <script type="text/javascript" src="../js/videok.js"></script>
</head>
<body>
    <h2><a href="#" style="font-size:15pt; color:#ff00e8; text-decoration: none;">Van  egy  jó  videód? Töltsd fel és kikerülhet az oldalra!</a></h2>
        <div id="videok"></div>
</body>        
</html>

And here is the Javascript for pulling

window.onload = initAll;
var xhr = false;
var dataArray = new Array();
var url = "choose url";

function initAll() {

if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
}
else {
    if (window.ActiveXObject) {
        try {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) { }
    }
}

if (xhr) {
    xhr.onreadystatechange = setDataArray;
    xhr.open("GET", url, true);
    xhr.send(null);
}
else {
    alert("couldn't create XMLHttpRequest");
}
}

function setDataArray() {
var tag1 = "subject1";
var tag2 = "subject2";

if (xhr.readyState == 4) {
    if (xhr.status == 200) {
        if (xhr.responseXML) {

            var allData = xhr.responseXML.getElementsByTagName(tag1);
            for (var i=0; i<allData.length; i++) {
                dataArray[i] = allData[i].getElementsByTagName(tag2)[0].firstChild.nodeValue;
            }
        }
    }
    else {
        alert("the request failed" + xhr.status);
    }
}
}

You won't be able to use javascript to pull from another web page because javascript is sandboxed when in browsers. Sandboxing means that you will only be able to send requests to the same domain that the javascript originally came from (also known as the 'same orgin policy').

You can use a serverside language like php to do the pulling and then hand it down to the javascript through ajax.

The code that you posted looks like it just makes a simple ajax call but it shouldn't work when trying to request an RSS from anything other than your own site.

It's better that you have the server side of your application fetch data for the xml and format the data how you want it.

You would have the Ajax request hit your server's end point, then your server will fetch the xml data, format it properly and respond to the request with the correct formatted data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM