0

I have a strange issue using jQuery and JSON, especially JSONP. My goal is to simply GET JSON data, but I always end up with the following error:

Uncaught SyntaxError: Unexpected token

Here is the code:

<script type="text/javascript">
     $(document).ready(function() {
      var myurl = "someurl";

     $.ajax({
            url: myurl,
            method: 'GET',
            contentType: 'application/javascript',
            dataType : 'jsonp',
            success: function(result){
                //Do something with JSON result
            }
    });
</script> 

And of course the JSON (raw format):

{"result":[{"targetView":"powerUsage","myData":{"someItems":["9","5","8"],"someItems2":[{"text":"protoText","currentRecord":"45.38","absolute":100}]}}]}

I tried the webservice with the Advanced Rest Client App in Google Chrome and it is working perfectly. I have no clue why this simple example gets this syntax error message.

2
  • Is your URL visible as I tried your code in jsfiddle and works fine : jsfiddle.net/repjt/693 Commented Aug 18, 2015 at 13:35
  • The URL itself is not visible to public. Just for info: I need JSONP to avoid the "access-control-allow-origin" error. Is it possible that I should upload my html file to a webserver and run it from there? I heared that running jQuery from C:\ can cause troubles. Commented Aug 18, 2015 at 13:39

2 Answers 2

1

Your Ajax code looks like fine. I think you are trying to make a Cross domain call as JSONP is a hack for dealing cross domain ajax call. If you Server code if ready for dealing with JSONP request then you must have send a callback parameter like

?callback=my_callback_method

than you service will return result with a callback see below links for more details:

https://learn.jquery.com/ajax/working-with-jsonp/
http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery
Sign up to request clarification or add additional context in comments.

4 Comments

The strange thing is that Google Chrome is showing me a correct JSON result set when I press on the URL next to the "Uncaught SyntaxError: Unexpected token :" message. Is the no way to manually override this error? Like, "Okay just give me the data so or so" He is definitly getting the correct data! As I mentioned my REST client plugin has no troubles loading this data, even if it is executed on my local computer.
well as I said, you might trying to connect a different domain that's why you are getting this error because you are not handling JSONP repose in a correct way that might correct in your client plugin anyway this is any exemption. Can you please share the URL which you trying to call from ajax?
I managed to do it with normal JSON by allowing the different domain to access the REST service (CORS server setting). However it is still a mystery for me how Google Chrome can see the nice JSON result, but it is not possible pass the result to my JavaScript. In good old C/C++ I can access whatever the machine gives me. :p
Great!! but if You need to use JSONP to make CROSS DOMAIN Requests. Please read: stackoverflow.com/questions/15005500/… stackoverflow.com/questions/11736431/…
0

You missed to put ready function close, that is }); at the last before script tag close:

<script type="text/javascript">
     $(document).ready(function()
     {
       var myurl = "someurl";

       $.ajax(
       {
         url: myurl,
         method: 'GET',
         contentType: 'application/javascript',
         dataType: 'jsonp',
         success: function(result)
         {
           //Do something with JSON result
         }
       });
     });
</script>

1 Comment

It was included ;) (seems that the posting ate the last part)

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.