0

At http://nwgerh.site88.net/default.php, the code is:

<html>
<body>
<?php

$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo json_encode($data);
?>
</body>
</html>

I'm trying to use Javascript to access the JSON from PHP. See my JSFiddle (based on this tutorial), but there are errors.

What's wrong? Is there a cross-domain issue?

9
  • I'm getting: Timestamp: SyntaxError: missing ) after argument list Commented Sep 6, 2012 at 17:38
  • ... have you tried adding a ) after the argument list? Commented Sep 6, 2012 at 17:39
  • Ok, I fixed that, but I'm still not getting anything: jsfiddle.net/XUTbs/1 Commented Sep 6, 2012 at 17:39
  • now it's a cross domain issue: XMLHttpRequest cannot load http://nwgerh.site88.net/default.php. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin. Commented Sep 6, 2012 at 17:40
  • ok, so any alternatives? Commented Sep 6, 2012 at 17:41

2 Answers 2

0

please post what error you're getting..

But your fiddle had an error anyway:

$.getJSON('http://nwgerh.site88.net/default.php', function(data) {
    document.write(data);
}​);​

It was missing the trailing )

it should work if it's on the same domain

5
  • It's not on the same domain, is that a problem? Commented Sep 6, 2012 at 17:40
  • you'll have to use jsonp then, or set the origin headers header('Access-Control-Allow-Origin: *');, as the other post said Commented Sep 6, 2012 at 17:43
  • Ok, I added header('Access-Control-Allow-Origin: *'); to the PHP file, and still nothing's happening. Commented Sep 6, 2012 at 17:45
  • I just tested it in jsFiddle using my example above... I get the response. Commented Sep 6, 2012 at 17:46
  • Sorry, I'm all over the place today. Can you link to your jsFiddle? Commented Sep 6, 2012 at 17:48
0

Try adding ?callback={} to the URL. This will force a JSONP AJAX request in jQuery -- which would eliminate your cross-domain issue.

2
  • Change {} to ? (question mark). Sorry. But, it is in fact a cross-domain issue and this solution should solve the problem. Commented Sep 6, 2012 at 17:44
  • Since your response is returning an HTML document, it won't appear using document.write (you can see it by viewing the source, but it won't show on screen). You'll need to parse the response data. Commented Sep 6, 2012 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.