简体   繁体   中英

jQuery ajax jsonp call always get unexpected token error

I've been wasting a great deal of time for something I think is so simple, yet there are no good examples that illustrate how and what the ajax call is doing.

This is my javascript code:

$.ajax({
type: 'GET',
url: 'https://maps.googleapis.com/maps/api/timezone/json',
dataType: 'jsonp',
data: {
    location: myLoc,
    timestamp: myTime,
    sensor: true
},
success: function(){alert('OK');},
error: function(){alert('FAIL');}
})
}

The variables "myLoc" and "myTime" are not the problem. In fact if I cut and paste the URI into my browser it works just fine and shows the data: https://maps.googleapis.com/maps/api/timezone/json?callback=jQuery18307430207263678312_1354817349576&location=36.7468422%2C-119.7725868&timestamp=1354817353&sensor=true&_=1354817353398

From what i've been reading, the "callback" parameter is automagically generated and somehow the code should be smart enough to call the success function or error function.

The error that chrome returns is "Uncaught syntax error unexpected token ':'" The javascript code always calls the error function no matter what I try. I added a jsonpCallback parameter (didn't work), json parameter (didn't work), changed dataType to "json" (didn't work due to cross domain error).

Please help.

$.ajax({
    type: 'GET',
    url: 'https://maps.googleapis.com/maps/api/timezone/json',
    dataType: 'jsonp',
    contentType: 'application/json; charset=utf-8',
    data: JSON.Stringify({
        location: myLoc,
        timestamp: myTime,
        sensor: true
    }),
    success: function(){alert('OK');},
    error: function(){alert('FAIL');}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM