2013-04-09 45 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');