有没有办法将更多数据传递给jQuery中的回调函数?
我有两个函数,我希望回调$.post,例如,传递AJAX调用的结果数据,以及一些自定义参数
function clicked() {
var myDiv = $("#my-div");
// ERROR: Says data not defined
$.post("someurl.php",someData,doSomething(data, myDiv),"json");
// ERROR: Would pass in myDiv as curData (wrong)
$.post("someurl.php",someData,doSomething(data, myDiv),"json");
}
function doSomething(curData, curDiv) {
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将自己的参数传递给回调,以及从AJAX调用返回的结果.
我在这里遵循规范,我不确定它是否允许使用多个参数调用onFulfilled.例如:
promise = new Promise(function(onFulfilled, onRejected){
onFulfilled('arg1', 'arg2');
})
Run Code Online (Sandbox Code Playgroud)
这样我的代码:
promise.then(function(arg1, arg2){
// ....
});
Run Code Online (Sandbox Code Playgroud)
会收到arg1和arg2?
我不关心任何具体的承诺如何实现它,我希望密切关注w3c规范的承诺.
我知道这是这是一个非常类似的问题一个.但在我的情况下,我仍然不清楚如何做到这一点.只需要一些成功回调的帮助.
这是有效的:
function getStuff(accountNumber) {
var logMessage = 'POST GetStuff';
return $http.post(GetStuff, { custId: accountNumber })
.then(log);
}
function log(response) {
logger.debug(response);
return response;
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要完成的事情:
function getStuff(accountNumber) {
var logMessage = 'POST GetStuff';
return $http.post(GetStuff, { custId: accountNumber })
.then(log(response, logMessage);
}
function log(response, logMessage) {
logger.debug(logMessage, response);
return response;
}
Run Code Online (Sandbox Code Playgroud)