有没有办法使用的JQuery的getJSON方法从外部网页HTML内容? [英] Is there any way to use the JQuery GetJSON method to get HTML from an external page?

查看:106
本文介绍了有没有办法使用的JQuery的getJSON方法从外部网页HTML内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,让我们说,你想要做一个jquery ajax请求,是这样的:

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

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

我理解,因为同样的原产地原则,该请求将失败,因为URL是外部域。

I understand that because of the same-origin principle, this request will fail because the URL is an external domain.

不过我听说的getJSON()不服从这一原则,并可以发送异步GET请求来使用JSONP和一个附加的URL。外部服务器

However I've heard that GetJSON() does not obey this principle and can send asynchronous get requests to external servers using JSONP and an appended URL.

我的问题是:是否有可能使用的getJSON()来检索来自外部的名字作为JSON对象在一个字符串中的所有HTML?如果默认情况下不这样做,有没有什么办法可以强制/诱骗成这样?

My question is: is it possible to use GetJSON() to retrieve all the HTML from an external name as a single string within a JSON object? If it doesn't do so by default, is there any way I can force / trick it into doing so?

推荐答案

是的,你可以从远程位置请求HTML,但是你必须使用代理来做到这一点。一个公开可用的代理是YQL。

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);
})

您当然可以建立自己的服务器返回纯HTML而不是JSON的。

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

这篇关于有没有办法使用的JQuery的getJSON方法从外部网页HTML内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆