Jon Lachonis
Jon Lachonis

Reputation: 931

JSONP - Can I Provide Data From PHP?

I'm trying to deal with cross-domain issues in javascript and am just beginning to learn about JSONP. I tried the following hoping it would work, but either I made a boo-boo or I don't fully understand the concept:

<script src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script> 
 $.getJSON( "https://somedomain.com/dash-beta/workers/ip.php?format=json&callback=?", function(data){ 
    alert (data.ip); 
     });

 </script>

The script on the other side is returning the data in this format:

({"ip":"0.0.0.0"})

But the alert never fires. For that matter, if I put "alert('woo!');' in its place, it doesn't fire. Where a I going wrong?

Upvotes: 1

Views: 68

Answers (1)

dev-null-dweller
dev-null-dweller

Reputation: 29462

On PHP side you should return passed callback:

echo $_GET['callback'] . '({"ip":"0.0.0.0"});';

Upvotes: 4

Related Questions