87,989
社区成员
发帖
与我相关
我的任务
分享$(function () {
$("#btnsure").on('click', function () {
var deviceinfo = getdeviceinfo();
debugger; //第三步,getdeviceinfo()方法调用完,返回的值为空
})
})
function getdeviceinfo() {
debugger;//第一步,断点先执行到这里,第二步直接就跳过ajax请求
var deviceinfo = { Model: "", Build: "", sysVersion: "", OS: "", fing: "", city: "" };
//获取当前城市信息
$.ajax({
url: 'https://api.map.baidu.com/location/ip?ak=VV??????????????&coor=bd09ll',
type: 'POST',
dataType: 'jsonp',
timeout: 2000,
async: false,
success: function (json) {
debugger; //,第四步,在前面onclick的事件执行完之后才中这个断点,正确返回了需要的数据
deviceinfo.city = json.content.address;
//此处省略其他代码
},
error: function (e) {
layer.open({ content: '错误!请刷新!', skin: 'msg', time: 2 })
},
complete: function (XMLHttpRequest, status) {
if (status == 'timeout') {
layer.open({ content: '网络不稳定,请求超时!', skin: 'msg', time: 2 })
}
}
})
debugger;//第二步,跳过了ajax请求
return deviceinfo;
}
$(function () {
$("#btnsure").on('click', async function () {
var deviceinfo = await getdeviceinfo();
debugger; //第三步,getdeviceinfo()方法调用完,返回的值为空
})
})
function getdeviceinfo() {
return new Promise((rs,rj) => {
debugger;//第一步,断点先执行到这里,第二步直接就跳过ajax请求
var deviceinfo = { Model: "", Build: "", sysVersion: "", OS: "", fing: "", city: "" };
//获取当前城市信息
$.ajax({
url: 'https://api.map.baidu.com/location/ip?ak=VV??????????????&coor=bd09ll',
type: 'POST',
dataType: 'jsonp',
timeout: 2000,
async: false,
success: function (json) {
debugger; //,第四步,在前面onclick的事件执行完之后才中这个断点,正确返回了需要的数据
deviceinfo.city = json.content.address;
//此处省略其他代码
rs(deviceinfo) //此处传什么 await接收到的就是什么
},
error: function (e) {
layer.open({ content: '错误!请刷新!', skin: 'msg', time: 2 })
},
complete: function (XMLHttpRequest, status) {
if (status == 'timeout') {
layer.open({ content: '网络不稳定,请求超时!', skin: 'msg', time: 2 })
}
}
})
debugger;//第二步,跳过了ajax请求
// return deviceinfo; 不需要return
})
}
$(function () {
$("#btnsure").on('click', async function () {
var deviceinfo = await getdeviceinfo();
debugger; //第三步,getdeviceinfo()方法调用完,返回的值为空
})
})
function getdeviceinfo() {
retrun new Promise((rs,rj) => {
debugger;//第一步,断点先执行到这里,第二步直接就跳过ajax请求
var deviceinfo = { Model: "", Build: "", sysVersion: "", OS: "", fing: "", city: "" };
//获取当前城市信息
$.ajax({
url: 'https://api.map.baidu.com/location/ip?ak=VV??????????????&coor=bd09ll',
type: 'POST',
dataType: 'jsonp',
timeout: 2000,
async: false,
success: function (json) {
rs(json)
debugger; //,第四步,在前面onclick的事件执行完之后才中这个断点,正确返回了需要的数据
deviceinfo.city = json.content.address;
//此处省略其他代码
},
error: function (e) {
layer.open({ content: '错误!请刷新!', skin: 'msg', time: 2 })
},
complete: function (XMLHttpRequest, status) {
if (status == 'timeout') {
layer.open({ content: '网络不稳定,请求超时!', skin: 'msg', time: 2 })
}
}
})
debugger;//第二步,跳过了ajax请求
return deviceinfo;
})
}
