更改
async为布尔值false。
http://api.jquery.com/jQuery.ajax/
var time;$.ajax({ async: false, type: 'GET', url: "http://www.timeapi.org/utc/now.json", success: function (data) { console.log(data); time = data; }, error: function (data) { console.log("ko"); }});console.log(time);另外,请注意,如果您需要在
dataType: 'jsonp'此处使用跨域,则将无法同步-请使用Promise。
var time;$.ajax({ dataType: 'jsonp', type: 'GET', url: "http://www.timeapi.org/utc/now.json", success: function (data) { time = data; }, error: function (data) { console.log("ko"); }}).then(function(){ // use a promise to make sure we synchronize off the jsonp console.log(time); });使用Q.js在此处查看这样的示例:
演示



