Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm developing something locally and running npm.

I tried using JavaScript to run a jsonp request on the api to get some data back.

I keep getting the same error and everything looks fine, although not sure what the problem is. Any help is appreciated.

Error Message: Uncaught SyntaxError: Unexpected token :

Note: I do not want to use jQuery for this at all

Here's my jsonp funciton

function jsonp(url, callback) {
   var script = document.createElement("script");
   script.src = url
   document.body.appendChild(script);
}

jsonp('/api/nav.json', function(data){
    console.log(data);
});

JSON File

{
   "items":[
      {
         "label":"Home",
         "url":"#/home",
         "items":[

         ]
      },
      {
         "label":"About",
         "url":"#/about",
         "items":[
            {
               "label":"What I eat",
               "url":"#/about/what-i-eat"
            },
            {
               "label":"How I Play",
               "url":"#/about/how-i-play"
            },
         ]
      },
      {
         "label":"Project",
         "url":"#/project",
         "items":[
         ]
      },
      {
         "label":"Ideas",
         "url":"#/ideas",
         "items":[
            {
               "label":"Cookies Club",
               "url":"#/ideas/cookies-club"
            },
            {
               "label":"Footie Boots",
               "url":"#/ideas/footie-boots"
            },
            {
               "label":"Books",
               "url":"#/ideas/books"
            }
         ]
      },
      {
         "label":"Contact",
         "url":"#/contact",
         "items":[
            {
               "label":"Email",
               "url":"#/contact/email"
            },
            {
               "label":"Phone",
               "url":"#/contact/phone"
            },
            {
               "label":"Snail Mail",
               "url":"#/contact/snail-mail"
            }
         ]
      }
   ]
}
share|improve this question
3  
A JSON response is not the same as a JSONP response. If your server is emitting the JSON you've listed then it's not JSONP – user1864610 Mar 1 '15 at 2:42
    
It's a json file. You can still make a jsonp request can't you? – patrick Mar 1 '15 at 2:42
1  
@patrick no, you need to make a regular json request. – m59 Mar 1 '15 at 2:43
    
@m59 care to explain? I'm genuinely curious. I thought JSONP was strictly just a way to call for cross-site requests. At least it's that in jQuery. – patrick Mar 1 '15 at 2:47
    
@patrick Yes, but JSONP is not the same as JSON. The server has to support it. If they were identical, JSONP would be redundant, after all. This will probably explain it ok niryariv.wordpress.com/2009/05/05/jsonp-quickly (just google, bro!) – m59 Mar 1 '15 at 2:49

You need to define a function that will be called by the newly loaded script. A jsonp return is live code to be run immediately, not a dead object like traditional json.

This code below works and is tested.

So something like this, where the callback is named theData:

function jsonp(url, callback) {
   var script = document.createElement("script");
   script.src = url
   document.body.appendChild(script);
}


theData = function(data){
    console.log(data);
}
jsonp('ok.js?callBack=theData');

Jquery creates the call back for you by passing it to the jsonp loader and adding the function to Window temporarily and then removing it after its been called just after sending it to the jsonp success function. In your case you can do the same, but I thought this simpler example would show the basics.

Then use PHP or the like to wrap the JSON in a function call. In PHP you can fetch the name of the function with $GET['callback'] and use that string as the function name of the function in which to wrap your data. Once the script is loaded it will attempt to run "theData," in this case which is a function available on Window. This will allow you to load local files if the html is also local, and allow you to open data from a different domain. So the output from the server is:

theData({ "items":[
      {
         "label":"Home",
         "url":"#/home",
         "items":[

         ]
      },
      {
         "label":"About",
         "url":"#/about",
         "items":[
            {
               "label":"What I eat",
               "url":"#/about/what-i-eat"
            },
            {
               "label":"How I Play",
               "url":"#/about/how-i-play"
            },
         ]
      },
      {
         "label":"Project",
         "url":"#/project",
         "items":[
         ]
      },
      {
         "label":"Ideas",
         "url":"#/ideas",
         "items":[
            {
               "label":"Cookies Club",
               "url":"#/ideas/cookies-club"
            },
            {
               "label":"Footie Boots",
               "url":"#/ideas/footie-boots"
            },
            {
               "label":"Books",
               "url":"#/ideas/books"
            }
         ]
      },
      {
         "label":"Contact",
         "url":"#/contact",
         "items":[
            {
               "label":"Email",
               "url":"#/contact/email"
            },
            {
               "label":"Phone",
               "url":"#/contact/phone"
            },
            {
               "label":"Snail Mail",
               "url":"#/contact/snail-mail"
            }
         ]
      }
   ]
})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.