jQuery: 1 ajax 请求和一个函数:传递参数给函数
jQuery: 1 ajax request and one function: pass parameters to function
我有一个按钮单击事件,我在其中执行了多个步骤。第一步是 ajax 请求,第二步是调用将按钮名称作为参数传递的函数。我该如何实现?
function buttonClick(e){
var param1ToBePassed = 1;
var param2ToBePassed = 2;
$.when(func1())
.done(function(){
$.when(
$.ajax({}),
//here I want to call func2 passing in param1tobepassed and param2tobepassed
funct2(param1ToBePassed, param2ToBePassed ) //this line isn't working
)
.done(()=>{console.log('ajax and func2 calls finished');})
}
}
var func1 = function(){var deferred = new $.Deferred(); /*do somethig;*/ return deferred;}
var func2 = function(param1, param2){var deferred = new $.Deferred(); /*do somethig with param1 and param2;*/ return deferred;}
如何将 param1ToBePassed 和 param2ToBePassed 传递给 func2?
function buttonClick(e){
var param1ToBePassed = 1;
var param2ToBePassed = 2;
$.when(func1())
.done(function(){
$.when(
$.ajax(),
//here I want to call func2 passing in param1tobepassed and param2tobepassed
funct2(param1ToBePassed, param2ToBePassed ) //this line isn't working
)
.done(function(){console.log('ajax and func2 calls finished');})
});/// I have corrected this added closeing )
}
var func1 = function(){var deferred = new $.Deferred(); /*do somethig;*/
return deferred;
}
var func2 = function(param1, param2){var deferred = new $.Deferred(); /*do somethig with param1 and param2;*/
return deferred;
}
你有一个语法错误,我已经在上面的代码中更正了它
既然你在利用jQuery,那你就可以使用jQuery的AJAX功能。它简化了很多逻辑并且很容易理解。
请注意,jQuery AJAX 函数默认是异步的。这意味着您可以同时执行任何其他任务。例如,如果您不想等待成功请求,您可以在任何地方调用函数。
function buttonClick(e) {
var param1ToBePassed = 1;
var param2ToBePassed = 2;
$.ajax({
url: 'YOUR_REQUEST_URL',
method: 'POST', // or GET
data: {
// any data to parse?
},
success:function(response){
func1(); // this will run on success.
}
});
func2(param1ToBePassed, param2ToBePassed); // this will run in parallel with the AJAX function.
}
AJAX函数的解释:
url: 这里是你定义请求路径的地方。
method:这里是你定义方法的地方,例如POST 或 GET 请求。
数据:在这里你可以根据你的请求解析一些数据(例如数据,或者你想要的任何东西)。
success:这是AJAX请求的成功函数。请求成功后会运行
我有一个按钮单击事件,我在其中执行了多个步骤。第一步是 ajax 请求,第二步是调用将按钮名称作为参数传递的函数。我该如何实现?
function buttonClick(e){
var param1ToBePassed = 1;
var param2ToBePassed = 2;
$.when(func1())
.done(function(){
$.when(
$.ajax({}),
//here I want to call func2 passing in param1tobepassed and param2tobepassed
funct2(param1ToBePassed, param2ToBePassed ) //this line isn't working
)
.done(()=>{console.log('ajax and func2 calls finished');})
}
}
var func1 = function(){var deferred = new $.Deferred(); /*do somethig;*/ return deferred;}
var func2 = function(param1, param2){var deferred = new $.Deferred(); /*do somethig with param1 and param2;*/ return deferred;}
如何将 param1ToBePassed 和 param2ToBePassed 传递给 func2?
function buttonClick(e){
var param1ToBePassed = 1;
var param2ToBePassed = 2;
$.when(func1())
.done(function(){
$.when(
$.ajax(),
//here I want to call func2 passing in param1tobepassed and param2tobepassed
funct2(param1ToBePassed, param2ToBePassed ) //this line isn't working
)
.done(function(){console.log('ajax and func2 calls finished');})
});/// I have corrected this added closeing )
}
var func1 = function(){var deferred = new $.Deferred(); /*do somethig;*/
return deferred;
}
var func2 = function(param1, param2){var deferred = new $.Deferred(); /*do somethig with param1 and param2;*/
return deferred;
}
既然你在利用jQuery,那你就可以使用jQuery的AJAX功能。它简化了很多逻辑并且很容易理解。
请注意,jQuery AJAX 函数默认是异步的。这意味着您可以同时执行任何其他任务。例如,如果您不想等待成功请求,您可以在任何地方调用函数。
function buttonClick(e) {
var param1ToBePassed = 1;
var param2ToBePassed = 2;
$.ajax({
url: 'YOUR_REQUEST_URL',
method: 'POST', // or GET
data: {
// any data to parse?
},
success:function(response){
func1(); // this will run on success.
}
});
func2(param1ToBePassed, param2ToBePassed); // this will run in parallel with the AJAX function.
}
AJAX函数的解释:
url: 这里是你定义请求路径的地方。
method:这里是你定义方法的地方,例如POST 或 GET 请求。
数据:在这里你可以根据你的请求解析一些数据(例如数据,或者你想要的任何东西)。
success:这是AJAX请求的成功函数。请求成功后会运行