-1

I'm downloading an external resource using a proxy on my server:

function retrieveOG(resource_url) {
    $.ajax({
        url: "/interne/asynch/fetchfeeds/FetchOG.aspx?resource=" + encodeURIComponent(resource_url),
        success: function (page) {
            var mypage = $page;
            console.log($page("meta[name='og:title']"));
        },
        error: function () {
            console.log("error");
        }
    });
}

basically, it send the url to my server, which download the page and return to the client the whole page. All of this because I need to catch the meta tags of a page.

But it says ReferenceError: $page is not defined. Where am I wrong?

4

1 回答 1

2

Pretty obvious really, $page is not defined. You have defined page instead.

I would suggest doing it like this:

success: function (page) {
    var $mypage = $(page);//convert your HTML string into a JQuery object
    console.log($mypage.filter("meta[name='og:title']").attr("content"));
}

That is making the assumption that page is an HTML string. It then converts that html string into a JQuery object called $mypage. This JQuery object can then be queried.

Because there is no parent wrapper element, we must use filter instead of find in this instance.

Here is a working example


If you want to use find instead, you can wrap the page in a parent tag like this:

var $mypage = $("<html/>").append(page);
console.log($mypage.find("meta[name='og:title']").attr("content"));
于 2013-10-11T08:42:51.373 回答