gpt4 book ai didi

javascript - 在已弃用的 javascript 函数中创建函数

转载 作者:行者123 更新时间:2023-12-01 05:19:03 25 4
gpt4 key购买 nike

目前我有一个 javascript 文件结构,对我来说似乎已弃用,在这个函数内有一个 ajax 调用,在 ajax 调用给出响应之后我想添加 ajax 函数,但如果我必须为每个定义它 1 by 1 ajax 响应类型,它会消耗大量空间,所以我需要创建一个函数来调用这个 ajax 函数,但我不知道在哪里放置我将创建的这个函数。这是我的代码

return Component.extend({
defaults: {
template: 'Icube_Snap/payment/snap'
},
redirectAfterPlaceOrder: false,
afterPlaceOrder: function() {
$.getScript(js, function() {
$.ajax({
type: 'post',
url: url.build('snap/payment/redirect'),
cache: false,
success: function(data) {
var token = data;
var methods = [];
var methodSnap = $('input[name=snap-method]:checked').val();

snap.pay(token, {
enabledPayments: methods,
onSuccess: function(result) {
$.ajax({ // <-- this ajax needs to be inside function with parameter
type: 'post',
url: url.build('custom/message/post'),
cache: false,
param: {
id: resut.id,
message: result.message
}
success: function(data) {

}
});
},
onPending: function(result) {
$.ajax({ // <-- this ajax needs to be inside function with parameter
type: 'post',
url: url.build('custom/message/post'),
cache: false,
param: {
id: resut.id,
message: result.message
}
success: function(data) {

}
});
},
onError: function(result) {
$.ajax({ // <-- this ajax needs to be inside function with parameter
type: 'post',
url: url.build('custom/message/post'),
cache: false,
param: {
id: resut.id,
message: result.message
}
success: function(data) {

}
});
},
onClose: function() {
$.ajax({ // <-- this ajax needs to be inside function with parameter
type: 'post',
url: url.build('custom/message/post'),
cache: false,
param: {
id: resut.id,
message: result.message
}
success: function(data) {

}
});
}
});
}
});
});
}
});

最佳答案

我刚刚向 POST 函数添加了一个 successcallback 和一个 errorcallback 。但如果您愿意,您可以忽略这些函数并在函数本身内部实现成功和错误功能,而无需使用回调。

return Component.extend({
defaults: {
template: 'Icube_Snap/payment/snap'
},
redirectAfterPlaceOrder: false,
afterPlaceOrder: function() {
$.getScript(js, function() {
$.ajax({
type: 'post',
url: url.build('snap/payment/redirect'),
cache: false,
success: function(data) {
var token = data;
var methods = [];
var methodSnap = $('input[name=snap-method]:checked').val();

//Define a function to send the POST request here.
//////////////////////////////////////////////////

var sendPayment = function(param, successcallback, errorcallback) {
$.ajax({ // <-- this ajax needs to be inside function with parameter
type: 'post',
url: url.build('custom/message/post'),
cache: false,
param: {
id: param.id,
message: param.message
}
success: function(data) {
successcallback(data);
},
error: function(error) {
errorcallback(error);
}
});
};

snap.pay(token, {
enabledPayments: methods,
onSuccess: function(result) {

//Call sendPayment method and you can
//pass whatever you want.

sendPayment(result, function() {
//Call when success
}, function() {
//Call when error
});
},
onPending: function(result) {
sendPayment(result, function() {
//Call when success
}, function() {
//Call when error
});
},
onError: function(result) {
sendPayment(result, function() {
//Call when success
}, function() {
//Call when error
});
},
onClose: function() {
sendPayment(result, function() {
//Call when success
}, function() {
//Call when error
});
}

});
}
});
});
}
});

关于javascript - 在已弃用的 javascript 函数中创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46485635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com