English 中文(简体)
$. Get not return html [关闭]
原标题:$.get not returning html [closed]

我的简单代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="jquery.js"></script>
<script type="text/javascript">
$(function(){
$.get("http://www.facebook.com",function(data){
alert(data);});

});</script></head>

 <body>
</body>
</html>

它应该提醒Facebook内容,但它没有这样做

最新更新

现在使用 php 的 iam

<?php
 $str=file_get_contents("http://www.facebook.com");
    echo strip_tags(htmlspecialchars($str)); 
?>

为什么它不删除标签?

最佳回答

如果您在服务器端使用 php 。 您可以创建一个返回 html 的服务 :

$html = file_get_contents( http://www.facebook.com );

ASP.NET:

using(WebClient client = new WebClient()) 
{
    string html = client.DownloadString("http://www.facebook.com ");
}
问题回答

我猜你刚刚因为Same First Policy 而被浏览器挡住。看控制台,如果尝试这样做,你会得到警告。

没有>Cross-Origin资源共享 或服务器旁的代理,您无法阅读跨域页面。

据我所知,这样做的唯一方法就是在您的域名 上有一个服务器脚本 读取外国页面( wget、 curl、fopen、 其它页面) 并将其返回到您的页面。 您的服务器就像一个“ 桥 ” 来读取远程页面 。

你可能听说过""http://en.wikipedia.org/wiki/JSONP" rel="nofollow">JSONP ,它没有跨域限制,但你正在检索的方法不是HTML/page,而是脚本。

除非使用JSONP, 否则您无法请求外部网站提供内容, 因为“ http:// en.wikipedia. org/ wiki/ Same_ blackin_ policy” rel = “ nofollow” > Same First Policy 。 但是, 您请求 HTML, 这不适用 。

选项是建立一个服务器侧端代理服务器, 以获取facebook. com 主页的 HTML, 然后提供本地 URL 到 $. get () 函数 。

您只能在同一域名下提出 ajax 请求

however you can use JSONP to make cross-domain requests
http://en.wikipedia.org/wiki/JSONP

Refer to this article on ibm developerworks:
http://www.ibm.com/developerworks/library/wa-aj-jsonp1/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.