I want to get the html of a website to check it for links, I use the following ajax code to get the html of a remote website:
$.ajax({
type : 'get',
url : 'proxy.php',
dataType : 'html',
success : function(data){
alert('success');
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert('error');
}
});
proxy.php is the proxy I use to get the data because it is not on my server :
<?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);
}
?>
the code always alert the error and I can not understand is everything is OK because I am not expert with ajax so I want anybody that could check that for me? is it wrong ajax datatype? is it wrong header in proxy file please???
You are serving the html file retrieved with proxy.php
with a wrong Content-type
.
The right content-type should be text/html
instead of application/html
.
If you browse to your proxy.php
file you would see that you are not getting any content and that your browser tries to download the page.
After the changing to the right content-type, if you browse to proxy.php
you would see the contents in your browser.
So, this is the line you've to change:
<?php
// Set your return content type
header('Content-type: text/html');