Is there any way to use the JQuery GetJSON method to get HTML from an external page?

前端 未结 3 565
野趣味
野趣味 2021-01-26 04:11

So let\'s say you\'re trying to do a jquery ajax request, something like:

$.ajax({
    ...
    url: http://other-website.com
    ...
})

I under

3条回答
  •  温柔的废话
    2021-01-26 04:39

    Yes, you can request html from a remote location, however you must use a proxy to do so. One publicly available proxy is YQL.

    http://jsfiddle.net/BKJWu/

    var query = 'SELECT * FROM html WHERE url="http://mattgemmell.com/2008/12/08/what-have-you-tried/" and xpath="//h1" and class="entry-title"';
    var url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=??";
    
    
    $.getJSON(url,function(data){
        alert(data.query.results.h1.content);
    })
    

    You could of course build your own on your server that returns plain html rather than json.

提交回复
热议问题