使用JSONP加载HTML网页 [英] Use JSONP to load an html page

查看:2354
本文介绍了使用JSONP加载HTML网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用加载外部网页 JSONP ,但网页是一个 HTML 页,我只是想抓住它使用AJAX的内容。

I'm trying to load an external page using JSONP, but the page is an HTML page, I just want to grab the contents of it using ajax.

编辑:为什么我这样做的原因是因为我想通过所有的用户信息,例如:接头,IP,代理,加载页面的时候,而不是我的服务器

The reason why I'm doing this is because I want to pass all the user information ex: headers, ip, agent, when loading the page rather than my servers.

这是可行的?现在,我可以得到的页面,但JSONP试图解析JSON,返回一个错误:未捕获的SyntaxError:意外的标记<

Is this doable? Right now, I can get the page, but jsonp attempts to parse the json, returning an error: Uncaught SyntaxError: Unexpected token <

样品code:

$.post('http://example.com',function(data){
    $('.results').html(data);
},'jsonp');

我已经设置了的jsfiddle的人来测试: http://jsfiddle.net/8A63A/1/

I've set up a jsfiddle for people to test with: http://jsfiddle.net/8A63A/1/

推荐答案

<一个href="http://en.wikipedia.org/wiki/JSONP#Script_element_injection">http://en.wikipedia.org/wiki/JSONP#Script_element_injection

拨打JSONP呼叫(换句话说,采用这种使用模式),   需要一个脚本元素。因此,对于每个新JSONP请求,   浏览器必须添加(或复用)了新的元素,换句话说,   注入元素到HTML DOM中,与用于所期望的值   src属性。然后这元件被评估,在src URL是   检索,且响应JSON进行评价。

Making a JSONP call (in other words, to employ this usage pattern), requires a script element. Therefore, for each new JSONP request, the browser must add (or reuse) a new element—in other words, inject the element—into the HTML DOM, with the desired value for the "src" attribute. This element is then evaluated, the src URL is retrieved, and the response JSON is evaluated.

现在看看你的错误:

未捕获的SyntaxError:意外的标记&LT;

Uncaught SyntaxError: Unexpected token <

&LT; 是所有的HTML标签的第一个字符,很可能这是&LT的开始; DOCTYPE ,在这种情况下,这是,当然的,无效的JavaScript

< is the first character of any html tag, probably this is the start of <DOCTYPE, in this case, which is, of course, invalid JavaScript.

,您不能使用JSONP用于读取HTML数据。

And NO, you can't use JSONP for fetching html data.

这篇关于使用JSONP加载HTML网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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