1

I am currently experimenting JSONP data using native Javascript. I am trying to get the data to display. How ever i am receiving a syntax error. Unexpected token : As far as i am aware i follow the correct steps into in order gather data. Below is a snippet of my code. Link to JSfiddle

<script src="http://linkToMyJSONDetails"></script>

JS

function (data) {
  var showStops = '';

 for (var i = 0; i < data.markers.length; i++) {
 showStops += '<div class="stops">';
 showStops += '<h3>' + data.markers[i].smsCode + '</h3>';
 showStops += '<h1>' + data.markers[i].name + '</h1>';
 showStops += '</div>';
 }

  document.getElementById('bus-stops').innerHTML = showStops;

 }
1
  • function (data) You need to give a name to the function. Also where are you calling this function? Just by adding the <script> add you can't access it using JavaScript. Commented Jul 21, 2015 at 17:27

3 Answers 3

1

You need to do 2 things. First: Add a callback: (Scroll to the right, since your link is a bit long)

http://digitaslbi-id-test.herokuapp.com/bus-stops?northEast=51.52783450,-0.04076115&southWest=51.51560467,-0.10225884&callback=someFunction
                                                                                                                     ^^^^^^^^^^^^^^^^^^^^^^   

Second: Define the callback:

// same function name as the callback in the jsonp url
function someFunction(data) { 
  var showStops = '';

  for (var i = 0; i < data.markers.length; i++) {
    showStops += '<div class="stops">';
    showStops += '<h3>' + data.markers[i].smsCode + '</h3>';
    showStops += '<h1>' + data.markers[i].name + '</h1>';
    showStops += '</div>';
  }

  document.getElementById('bus-stops').innerHTML = showStops;

}

Fiddle: http://jsfiddle.net/29eajsjm/4/ Make sure the function is defined before calling the jsonp.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this VERY helpful, i didn't realise how simple is it.
0

you have not mentioned the function name.

insead of

function (data) {

use

var myFn = function (data) {

};

or

function myFN(data) {

}

Comments

0

First of all validate the json you are getting from your function at jsonlint.

Comments

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.