我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud) 我想使用promises,但我有一个回调API,格式如下:
window.onload; // set to callback
...
window.onload = function() {
};
Run Code Online (Sandbox Code Playgroud)
function request(onChangeHandler) {
...
}
request(function() {
// change happened
...
});
Run Code Online (Sandbox Code Playgroud)
function getStuff(dat, callback) {
...
}
getStuff("dataParam", function(err, data) {
...
})
Run Code Online (Sandbox Code Playgroud)
API;
API.one(function(err, data) {
API.two(function(err, data2) {
API.three(function(err, data3) {
...
});
});
});
Run Code Online (Sandbox Code Playgroud)
有一个用于从JavaScript发出请求的新API:fetch().是否有任何内置机制可以在飞行中取消这些请求?
完全披露:我有资格拥有中级JavaScript知识.所以这略高于我此时的经验水平.
我有一个Google Chrome扩展程序,只要file:///页面加载就会为本地执行AJAX请求.在我从请求中得到响应之后,我将在代码中使用多个函数中返回的代码.大部分时间我都会在需要运行的代码之前收到响应.但有时我不会,一切都会破裂.
现在,我假设我可以抛出下面的所有相关代码xhr.onload.但这似乎效率低下?我有许多依赖于响应的活动部件,将它们全部放在那里似乎很糟糕.
我已经阅读了几篇与async/await相关的文章,并且我在理解这个概念时遇到了麻烦.我也不是100%肯定我正在以正确的方式看待这个问题.我是否应该考虑使用async/await?
这是我的AJAX请求的代码.
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function(e) {
code = xhr.response;
};
xhr.onerror = function () {
console.error("** An error occurred during the XMLHttpRequest");
};
xhr.send();
Run Code Online (Sandbox Code Playgroud)
假设我后来在我的代码中有一些需要触发的函数.现在他们看起来像:
function doTheThing(code) {
// I hope the response is ready.
}
Run Code Online (Sandbox Code Playgroud)
什么是最好的方法来解决这个问题?仅供参考,FetchAPI不是一种选择.
这是我的代码结构的高级视图.
// AJAX request begins.
// ...
// A whole bunch of synchronous code that isn't dependant on
// the results of my AJAX request. …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我有两个XMLHttpRequest()对象,比如A和B.
我想要完成的是当A完成获取数据项列表时,将触发B以根据以前获取的先前数据项获取更多项.
目前我的问题是两个对象彼此独立工作.
我的代码如下:
var A = new XMLHttpRequest();
var B = new XMLHttpRequest();
A.open("GET", directory, true);
A.onreadystatechange = function () {
if (A.readyState === 4) {
if (A.status === 200 || A.status == 0) {
//does... something
}
}
}
A.send(null);
while(true){
B.open("GET", another_directory, false);
B.overrideMimeType("application/document");
B.send(null);
if (B.status == "404")
continue;
//does... something else
}
Run Code Online (Sandbox Code Playgroud)
此代码无法正常工作,因为我发现在A完成之前,evertime B会继续进行.我基本上不知道要使用哪个事件.
我怎样才能实现目标?我可以使用哪些事件以便在完成A后立即同步处理B.
v-html 为模态接口,内容来自一个URL(阿贾克斯)...的剪断代码运行,但它是坏的,不专业:尝试运行.你点击GO,收到一条消息说关闭并再次点击GO ... 问题:如何更好地使用Vuetify这个界面?
我需要一键式(GO)界面,并且需要用户的最短等待时间.
(还需要加载其他URL,所以使用AJAX多次作为其他按钮等)
我假设Vuetify有很好的AJAX加载方法(就像这些),所以我需要使用最好的方法!对于这种情况.
PS:我没有看到如何在单个HTML页面(不是一个大的Node.js系统)中实现Vue-AJAX策略.
它正在运行,但正在混合非VueJS代码并具有所有已解释的问题,由答案修复.
var URL = 'https://codepen.io/chriscoyier/pen/difoC.html';
var loadFromUrl_cache = '... PLEASE WAYTE 10 seconds ...'+
'<br/>close and back here.';
function loadFromUrl(URL,tmpStore) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
loadFromUrl_cache = xmlhttp.responseText;
} else {
alert('ERROR, something else other than status …Run Code Online (Sandbox Code Playgroud)javascript ×5
ajax ×3
node.js ×2
async-await ×1
asynchronous ×1
bluebird ×1
callback ×1
fetch-api ×1
jquery ×1
npm ×1
promise ×1
vuetify.js ×1