0

Web サイトの html を取得してリンクをチェックしたいので、次の ajax コードを使用してリモート Web サイトの 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);
}
?>

コードは常にエラーを警告しますが、私は ajax の専門家ではないため、すべてが問題ないことを理解できません。それは間違ったajaxデータ型ですか?プロキシファイルのヘッダーが間違っていますか???

4

1 に答える 1

0

proxy.php間違った で取得した html ファイルを提供していContent-typeます。

正しい content-type はtext/htmlの代わりにある必要がありapplication/htmlます。

ファイルを参照するproxy.phpと、コンテンツが何も取得されておらず、ブラウザーがページをダウンロードしようとしていることがわかります。

適切なコンテンツ タイプに変更した後、proxy.php参照すると、ブラウザにコンテンツが表示されます。

したがって、これは変更する必要がある行です。

<?php
// Set your return content type
header('Content-type: text/html');
于 2013-04-09T09:30:20.767 に答える