Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61832

How to simply read html from any url via javascript and proxy?

This is my proxy.php file:

$url = urldecode($_GET['url']);
$url = 'http://' . str_replace('http://', '', $url); // Avoid accessing the file system
echo file_get_contents($url); // You should probably use cURL. The concept is the same though

and my reader.js:

$(document).ready(function () {

    var url = 'http://localhost/taboo.blue-world.pl/admin/proxy.php?url=' + encodeURIComponent('http://www.blue-world.pl')

    $.ajax({
        url      : url,
        type     : 'GET',
        dataType : 'json'
    }).done(function(data) {
        console.log(data.results.result[1].category); // Do whatever you want here
    });
});

But it doesnt print anything. Can you help me solve it? I am not quite good with this.

Upvotes: 1

Views: 222

Answers (4)

aish123
aish123

Reputation: 141

Use either dataType: 'html' in reader.js (to get HTML data)

OR

echo(json_encode(file_get_contents($url))); in proxy.php (for JSON data)

Upvotes: 1

Amit Visodiya
Amit Visodiya

Reputation: 813

Try jQuery.parseJSON for json format

 $.ajax({
        url      : url,
        type     : 'GET',
        dataType : 'json'
    }).done(function(data) {
        var data = jQuery.parseJSON(data);
        console.log(data.results.result[1].category); // Do whatever you want here
    });
    });

Upvotes: 0

Davide Rossi
Davide Rossi

Reputation: 516

It looks like you are trying to get an HTML response as JSON.

If the content is HTML you should turn you ajax call to:

$.ajax({
    url      : url,
    type     : 'GET',
    dataType : 'html'
}).done(function(data) {
    console.log(data); // data contains the html as a text
});

Upvotes: 1

Rene Korss
Rene Korss

Reputation: 5484

Currently your are trying to get JSON response. Change dataType to html.

dataType: 'html'

Upvotes: 1

Related Questions