123
123

Reputation: 8951

JSONP ReferenceError: Can't find variable

I'm trying to set up an incredibly simple JSONP callback.

<script src="http://marsweather.ingenology.com/v1/latest/?callback=data&format=jsonp">
   var response = data()
   console.log response
</script>

This gives me the following error:

ReferenceError: Can't find variable: data

JSONP has been enabled per this request on Github

Am I doing something wrong here?

Upvotes: 0

Views: 1282

Answers (1)

Barmar
Barmar

Reputation: 780909

In JSONP, you're supposed to define the callback function in a script on the client page before it tries to load the remote script. The server sends back Javascript that calls this function with the JSON data.

Also, you can't put code in a <script> tag that has a src attribute.

So your code should look like:

<script type="text/javascript">
function data(response) {
    console.log(response);
}
</script>
<script src="http://marsweather.ingenology.com/v1/latest/?callback=data&format=jsonp"></script>

Upvotes: 2

Related Questions