简体   繁体   English

jQuery ajax jsonp调用始终会出现意外的令牌错误

[英]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. 我一直在浪费很多时间,因为我认为这很简单,但是没有很好的示例来说明ajax调用的方式和作用。

This is my javascript code: 这是我的JavaScript代码:

$.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. 变量“ myLoc”和“ myTime”不是问题。 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 事实上,如果我将URI剪切并粘贴到浏览器中,它就可以正常工作并显示数据: https : //maps.googleapis.com/maps/api/timezone/json?callback=jQuery18307430207263263678312_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. 从我一直在阅读的内容中,会自动生成“ callback”参数,并且某种程度上,代码应该足够聪明以调用成功函数或错误函数。

The error that chrome returns is "Uncaught syntax error unexpected token ':'" The javascript code always calls the error function no matter what I try. chrome返回的错误是“未捕获的语法错误意外标记':'”,无论我如何尝试,javascript代码始终会调用错误函数。 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). 我添加了一个jsonpCallback参数(无效),json参数(无效),将dataType更改为“ json”(由于跨域错误而无效)。

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');}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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