0

I use php with file_get_contents and works perfectly for get some results from url, but if use load for show the page and results with jQuery, doesn't show anything.

I have one page that give me some results I need, I call page from url, and show the result.

When call from other domain, etc, show me the content and these results, for example :

$results=file_get_contents("http://loadwebdata.com/results.php");

results.php call to these results using curl.

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); 
$result = curl_exec($ch);
curl_close($ch);
return $result;

And all works fine, but the only problem is when I want show these results for example using load from jQuery:

jQuery(document).ready(function() {

jQuery("#loader").show("10").load("http://loadwebdata.com/results.php");

});

<div id="loader"></div>

In this case, with jQuery doesn't show anything, by this the question, because the result haven't any format, is simple text and I understand from jQuery must show without any problem, but no, it won't show.

Zak
  • 6,976
  • 2
  • 26
  • 48
Jean
  • 13
  • 3
  • Are you sure this problem is related to jQuery after all? Did you inspect your browser's developer console for problems? Does the request succeed? Does it contain the expected data? – Nico Haase Jul 07 '23 at 15:36
  • With file_get_contents from php works, and also if test the script with jquery from the same domain that give me the results jQuery works, the problem is when use the jQuery script from localhost and call the domain for get results, etc, but if use from other domain works, but no works from localhost in Appserver or Xamp – Jean Jul 07 '23 at 15:43
  • Please add all clarification to your question by editing it, not to the comment section – Nico Haase Jul 10 '23 at 05:54

1 Answers1

0

Based on your latest comment:

With file_get_contents from php works, and also if test the script with jquery from the same domain that give me the results jQuery works, the problem is when use the jQuery script from localhost and call the domain for get results, etc, but if use from other domain works, but no works from localhost in Appserver or Xamp

This looks like an issue with CORS

Check out your browser developer console. If it looks like this, we can confirm that it is an issue with CORS:

CORS troubleshooting The solution is to update your PHP code, or your Apache configuration on the server side (or whatever webserver you are using)

To add the following headers to your HTTP response:

Access-Control-Allow-Origin: '*' (or localhost, for better security)
Access-Control-Allow-Methods: 'POST, GET, OPTIONS'

Then the browser should allow requests to your webserver from your localhost server, or depending if you use the * sign in the Access-Control-Allow-Origin, from anywhere.

Here you can have more details about CORS at the apache2 level

Or this link if you prefer doing this in PHP directly

I can update this answer if I can get any further details about the issue you are having.

Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
  • 1
    Yes i fix this put in the http header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: *");, and now jquery works, nice solution, thank´s a lot – Jean Jul 07 '23 at 20:06
  • No problem, glad to help :) – Lucas Meine Jul 08 '23 at 14:25