0

Heres my ajax request

$.ajax({
    url: req_url,
    contentType: 'application/json',
    type: 'GET',
    dataType:'jsonp',
    success: function(data){
        console.log(data);
    },
})

and the error

?id=BLC&format=json&starttime=2016-09-13&callback=jQuery3100840…
1473881883047&=147388…:1 Uncaught SyntaxError: Unexpected token :

to remove all the jquery cached appended on the end I used this code

$.ajax({
    url: req_url,
    contentType: 'application/json',
    type: 'GET',
    dataType:'jsonp',
    jsonp: false,
    jsonpCallback: 'callback',
    cache: true,
    success: function(data){
        console.log(data);
    },
})

and the error here is the same

?id=BRD&format=json&starttime=2016-09-13:1 Uncaught SyntaxError: Unexpected token :

D.Dimitrioglo
  • 3,413
  • 2
  • 21
  • 41
  • That should be ignored. It's called a 'cache-buster' and is harmless. It's just changing the URL just enough that it bypasses the cache. – Native Coder Sep 14 '16 at 19:48
  • Possible duplicate of [Why does jQuery.ajax() add a parameter to the url?](http://stackoverflow.com/questions/2749560/why-does-jquery-ajax-add-a-parameter-to-the-url) – Native Coder Sep 14 '16 at 19:48
  • @NativeCoder That doesn't look like the cache buster. It gets added as an additional parameter, it doesn't modify the existing parameters. – Barmar Sep 14 '16 at 19:50
  • Why do you have a `contentType:` option when there's no `data:` option? And `contentType:` is meaningless for `GET` requests, since there's no contents. – Barmar Sep 14 '16 at 19:51
  • I looked at that one and removed the cache_buster it in my second request, but it wouldn't cause an error. – jasminder88 Sep 14 '16 at 19:52
  • I believe the OP is talking about the `:1` that is getting added to the end of the url after the parameters. – Pedro Estrada Sep 14 '16 at 19:52
  • yeah i dont know, I was just trying to add things to make it start working – jasminder88 Sep 14 '16 at 19:53
  • 2
    Adding something to the URL doesn't cause a syntax error. I suspect the real problem is that the server doesn't actually implement JSONP. – Barmar Sep 14 '16 at 19:53
  • heres the link to the api http://www.geomag.nrcan.gc.ca/test/ws/data/?id=BLC&format=json&starttime=2015-04-03 , when I tried without dataType=jsonp it came back with access-control-allow-origin error – jasminder88 Sep 14 '16 at 19:54
  • When you use JSONP, the server is supposed to send back Javascript code that calls the callback function with the JSON data. If the response isn't a valid script, you'll get this error. – Barmar Sep 14 '16 at 19:54
  • That API doesn't suppose JSONP. You need to call the API from your server, not the client. See http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about?s=1|8.9018 – Barmar Sep 14 '16 at 19:56
  • `dataType: 'jsonp'` is not a magic solution to cross-origin issues, it requires special support on the server. – Barmar Sep 14 '16 at 19:56
  • okay makes sense thanks – jasminder88 Sep 14 '16 at 20:02

1 Answers1

0

@Bamar is correct about the cache-buster. I believe it is because you are using jsonp. Try just using

datatype: json

Unless, you NEED a cross-domain request...

EDIT:

Instead of URL encoding the parameters yourself, let jquery do it for you. try something like

$.ajax({
url: 'http://www.foo.com/',
data: YourData.serilize(),
contentType: 'application/json',
type: 'GET',
dataType:'jsonp',
success: function(data){
    console.log(data);
},
})
Native Coder
  • 1,792
  • 3
  • 16
  • 34