Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  152] [ 7]  / answers: 1 / hits: 26705  / 13 Years ago, wed, december 14, 2011, 12:00:00

I am trying to get HTML code from a webpage that is not in the same domain. The html text is parsed & summarises a recipe(recipe name, main ingredients, no. of steps) found on that page the HTML code was from.



The user can then click the link & go to that webpage outside the domain to view the recipe.



I'm aware of the Same-Origin-Policy, but does that apply to getting HTML code from a webpage outside the domestic domain? I imagine its exactly the same as getting XML, so this is legal & allowed isn't it?



Is there a way I can get the HTML text/code from a domain outside my domestic domain?



Using Javascript & JQuery, the idea is to limit the amount of server requests & storage by having the user perform requests for each recipe & parsing the HTML on the client side. This stops server side bottlenecks & also means I dont have to go through the server & delete old outdated recipe summarisations.



I'm open to Solutions/Suggestions in any programming language or API or etc.


More From » jquery

 Answers
8

What you are trying to do can't be done using any AJAX library. Browsers' cross-domain policy won't allow you to do this.



But you can do this with a combination of php (or any other server-side language) and AJAX. Create a php script like this:



<?php
$url=$_POST['url'];
if($url!=)
echo file_get_contents($url);
?>


Let us say the script's name is fetch.php.
Now you can throw an AJAX call from your jQuery code to this fetch.php and it will fetch the HTML code for you.


[#88567] Monday, December 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tomas

Total Points: 165
Total Questions: 111
Total Answers: 103

Location: Maldives
Member since Tue, Dec 21, 2021
2 Years ago
tomas questions
Thu, Jan 27, 22, 00:00, 2 Years ago
Mon, May 10, 21, 00:00, 3 Years ago
Tue, Jan 5, 21, 00:00, 3 Years ago
;