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.

spenibus
  • 4,339
  • 11
  • 26
  • 35
J0eBl4ck
  • 415
  • 1
  • 4
  • 10
  • Is your URL visible as I tried your code in jsfiddle and works fine : http://jsfiddle.net/repjt/693/ – Zaki Aug 18 '15 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. – J0eBl4ck Aug 18 '15 at 13:39

2 Answers2

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
Yogendra
  • 2,139
  • 3
  • 14
  • 27
  • 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. – J0eBl4ck Aug 18 '15 at 14:17
  • 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? – Yogendra Aug 19 '15 at 05:49
  • 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 – J0eBl4ck Aug 19 '15 at 12:09
  • Great!! but if You need to use `JSONP` to make CROSS DOMAIN Requests. Please read: http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-jquery-ajax/17299796#17299796 http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery – Yogendra Aug 19 '15 at 12:44
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>
Dhruv
  • 173
  • 11