ajax 异步弃用功能的替代方案是什么?
what alternative for ajax async deprecated feature?
使用以下 ajax 函数调用 API 端点,JSON 格式的 returns 数据不会将 results
的值从未定义更改json['info']
中的内容,但是当将 async
更改为 false 时,它确实如此。
检查包含此功能的网页,它显示此功能已被弃用,没有任何其他建议。
如何在不编写更多代码来解析从服务器返回的数据的情况下读取 json 数据?
function update_info()
{
var results ;
$.ajax({
type: 'GET',
url : "ip:port/api/info",
async:true,
success: function(data) {
results = data['info'];
//console.log(results);
}
});
console.log(results);
return results;
}
似乎不符合我的目标,因为数据是从 API 返回的,而不是网页源代码返回到 ajax。
正如 David 所说,您应该使用异步调用
function update_info()
{
return $.ajax({
type: 'GET',
url : "ip:port/api/info",
async:true,
success: function(data) { data;}
});}
async function f1() {
var results = await update_info();
console.log(results['info']);
}
f1();
这将在完成后 return ajax 调用,然后您可以从中获取 info
数据
您可以使用这两种方法,并且可以使用 .then
或 await
。
我附上了以下示例的源代码。
ajaxRequest("https://httpbin.org/ip").then(result => {
console.log('Your PC Address is:' + result.origin);
});
let myPcRes = await ajaxRequest("https://httpbin.org/ip");
console.log('Your PC Address is from the await approach: ' + myPcRes.origin);
您需要将 results
与 $.ajax
进行比较
和return成功方法的响应。
希望对您有所帮助,
https://jsfiddle.net/tru5k6qz/
使用以下 ajax 函数调用 API 端点,JSON 格式的 returns 数据不会将 results
的值从未定义更改json['info']
中的内容,但是当将 async
更改为 false 时,它确实如此。
检查包含此功能的网页,它显示此功能已被弃用,没有任何其他建议。
如何在不编写更多代码来解析从服务器返回的数据的情况下读取 json 数据?
function update_info()
{
var results ;
$.ajax({
type: 'GET',
url : "ip:port/api/info",
async:true,
success: function(data) {
results = data['info'];
//console.log(results);
}
});
console.log(results);
return results;
}
正如 David 所说,您应该使用异步调用
function update_info()
{
return $.ajax({
type: 'GET',
url : "ip:port/api/info",
async:true,
success: function(data) { data;}
});}
async function f1() {
var results = await update_info();
console.log(results['info']);
}
f1();
这将在完成后 return ajax 调用,然后您可以从中获取 info
数据
您可以使用这两种方法,并且可以使用 .then
或 await
。
我附上了以下示例的源代码。
ajaxRequest("https://httpbin.org/ip").then(result => {
console.log('Your PC Address is:' + result.origin);
});
let myPcRes = await ajaxRequest("https://httpbin.org/ip");
console.log('Your PC Address is from the await approach: ' + myPcRes.origin);
您需要将 results
与 $.ajax
和return成功方法的响应。
希望对您有所帮助, https://jsfiddle.net/tru5k6qz/