Didier Levy
Didier Levy

Reputation: 3453

jQuery load a foreign web page with xmlHttpReq and parse the resulting variable as a DOM document

Trying to figure out how to do this:

Within the $(doculent).ready function I use this code to read a web page from another server:

var qString = "Newsletters/Legacy/somewebpage.html";

        var xmlHttpReq = createXMLHttpRequest();
        xmlHttpReq.open("GET", qString, false);
        xmlHttpReq.send(null);
        var answer = xmlHttpReq.responseText;

Now I try to access the DOM Document elements from that answer variable:

var a = $('answer.lnkbuttontext:first').text();
        $('#theMenu').html = a;

Where elements from class lnkbuttontext are links and I want both the link and the likn text.

Trying with ...text() returns empty string and trying with ...html() returns undefined.

I must be wrong somewhere...

Upvotes: 0

Views: 129

Answers (1)

idbehold
idbehold

Reputation: 17168

You cannot make cross-domain XHRs unless the server you're requesting data from includes the proper Access-Control headers in its response. Without those headers you will have to use a proxy to make the request.

Upvotes: 2

Related Questions