2013-04-09 57 views
0

我希望得到一个网站的HTML来检查它的链接, 我用下面的Ajax代码来获得一个远程网站的HTML:Ajax和HTML问题

$.ajax({ 
    type : 'get', 
    url : 'proxy.php', 
    dataType : 'html', 
    success : function(data){ 
    alert('success'); 
    }, 
    error : function(XMLHttpRequest, textStatus, errorThrown) { 
    alert('error'); 
    } 
}); 

proxy.php是代理我用它来获取数据,因为它不是我的服务器上:

<?php 
// Set your return content type 
header('Content-type: application/html'); 

// Website url to open 
$daurl = 'http://example.com'; 
// Get that website's content 
$handle = fopen($daurl, "r"); 

// If there is something, read and return 
if ($handle) { 
    while (!feof($handle)) { 
     $buffer = fgets($handle, 4096); 
     echo $buffer; 
    } 
    fclose($handle); 
} 
?> 

代码总是提醒错误,我无法理解的是一切都OK,因为我不是专家,阿贾克斯,所以我想任何人都可能检查我的?它是错误的ajax数据类型?代理文件中的头文件是否错误?

+1

在你的误差函数试图提醒textStatus或errorThrown而不是字符串说错误,告诉我们是什么错误。 – piddl0r 2013-04-09 09:06:48

+0

textStatus警报错误,但errorThrown警报未找到 – 2013-04-09 09:12:31

+0

你检查了开发者控制台吗? – piddl0r 2013-04-09 09:30:29

回答

0

您正在提供与proxy.php检索的HTML文件的错误Content-type

正确的内容类型应该是text/html而不是application/html

如果您浏览到您的proxy.php文件,您会看到您没有收到任何内容,并且您的浏览器尝试下载该页面。

更改为正确的内容类型后,如果您浏览到proxy.php,您会在浏览器中看到内容。

所以,这是你已经改变线路:

<?php 
// Set your return content type 
header('Content-type: text/html');