Okay lets begin,
好的,让我们开始,
I have created an ajax namespace in javascript that just made it easier to remember what values needed to be inserted in-order to get it to work.
我在javascript中创建了一个ajax命名空间,这使得更容易记住需要插入的值以使其工作。
(function (ns) {
ns.type = "POST";
ns.url = "/";
ns.data = {};
ns.success = function() {};
ns.send = function() {
$.ajax({
type: ns.type,
url: ns.url,
data: ns.data,
success: ns.success()
});
}
}(fps.ajax));
to make use of it I do the following
为了利用它,我做了以下几点
var ajax = fps.ajax;
ajax.url = "/credit/getBalance";
ajax.type = "GET";
ajax.success = function (e) {
navCredits.text(navCredits.text().f(e));
};
ajax.send();
now the problem I'm having is my success function.
现在我遇到的问题是我的成功功能。
I pass it a varible that I want my ajax function to use as it's return data but it's not happening in that way.
我传递了一个变量,我希望我的ajax函数可以使用它作为返回数据,但它不会以这种方式发生。
My understanding of this is that it's (e) not declaired where the success function is being set, hence why I'll get an undefined value.
我对此的理解是,(e)没有声明成功函数的设置位置,因此为什么我会得到一个未定义的值。
My question is, is there a way to pass a function with a "parameter" that the function should use for it's own functions value?
我的问题是,有没有办法传递一个带有“参数”的函数,该函数应该使用它自己的函数值?
I don't feel like I'm explaining very well but hopefully there's enough code there to help you understand what I'm trying to achieve.
我不觉得我解释得很好,但希望有足够的代码可以帮助你理解我想要实现的目标。
Kind regards
1 个解决方案
#1
2
You're executing your success function instead of assigning it as the handler. Remove the () off the "success:ns.success()" line.
您正在执行您的成功函数,而不是将其指定为处理程序。从“success:ns.success()”行中删除()。
ns.send = function() {
$.ajax({
type: ns.type,
url: ns.url,
data: ns.data,
success: ns.success // you don't want to execute
});
}
When you put the "()" after a function reference, it will execute it at that moment. In your case, it's executing an empty function, as defined a few lines up. Your success function with the arguments for the event is declared fine, but never being used.
当你在函数引用之后放入“()”时,它将在那一刻执行它。在你的情况下,它正在执行一个空函数,如几行所定义。使用事件参数的成功函数被声明为正常,但从未被使用过。
When you remove the "()", it assigns the function object to the property. When you add the "()" it executes the function at that moment and assigns the return value to the property. And when the jQuery "ajax" call is completed successfully, the success function is executed on that object, so they handle the adding of the "()" at that time. ;)
删除“()”时,它会将该函数对象分配给该属性。当你添加“()”时,它会在那一刻执行函数并将返回值赋给属性。当jQuery“ajax”调用成功完成时,将在该对象上执行success函数,因此它们在那时处理添加“()”。 ;)
#1
2
You're executing your success function instead of assigning it as the handler. Remove the () off the "success:ns.success()" line.
您正在执行您的成功函数,而不是将其指定为处理程序。从“success:ns.success()”行中删除()。
ns.send = function() {
$.ajax({
type: ns.type,
url: ns.url,
data: ns.data,
success: ns.success // you don't want to execute
});
}
When you put the "()" after a function reference, it will execute it at that moment. In your case, it's executing an empty function, as defined a few lines up. Your success function with the arguments for the event is declared fine, but never being used.
当你在函数引用之后放入“()”时,它将在那一刻执行它。在你的情况下,它正在执行一个空函数,如几行所定义。使用事件参数的成功函数被声明为正常,但从未被使用过。
When you remove the "()", it assigns the function object to the property. When you add the "()" it executes the function at that moment and assigns the return value to the property. And when the jQuery "ajax" call is completed successfully, the success function is executed on that object, so they handle the adding of the "()" at that time. ;)
删除“()”时,它会将该函数对象分配给该属性。当你添加“()”时,它会在那一刻执行函数并将返回值赋给属性。当jQuery“ajax”调用成功完成时,将在该对象上执行success函数,因此它们在那时处理添加“()”。 ;)