Node.js Can';t调用成功函数
我正在用Express.js应用程序开发一个主干节点.js。我试图在收到服务器的响应后触发一个函数,如下面的代码所示: mainView.jsNode.js Can';t调用成功函数,node.js,backbone.js,express,Node.js,Backbone.js,Express,我正在用Express.js应用程序开发一个主干节点.js。我试图在收到服务器的响应后触发一个函数,如下面的代码所示: mainView.js ... user: null, initialize: function () { _.bindAll(this, 'registerSuccess', 'loginSuccess'); this.user = new userModel(); } registerSuccess: function () { this
...
user: null,
initialize: function () {
_.bindAll(this, 'registerSuccess', 'loginSuccess');
this.user = new userModel();
}
registerSuccess: function () {
this.user.auth({
success: this.loginSuccess
});
});
loginSuccess: function () {
console.log('loginSuccess');
}
...
, auth: function (options) {
$.ajax({
url: 'users/auth'
, type: 'post'
}, options);
})
var api = require('./routes/apiController');
app.post('/users/auth', api.userAuth);
exports.userAuth = function (req, res) {
return res.send( {
status: 200
});
}
userModel.js
...
user: null,
initialize: function () {
_.bindAll(this, 'registerSuccess', 'loginSuccess');
this.user = new userModel();
}
registerSuccess: function () {
this.user.auth({
success: this.loginSuccess
});
});
loginSuccess: function () {
console.log('loginSuccess');
}
...
, auth: function (options) {
$.ajax({
url: 'users/auth'
, type: 'post'
}, options);
})
var api = require('./routes/apiController');
app.post('/users/auth', api.userAuth);
exports.userAuth = function (req, res) {
return res.send( {
status: 200
});
}
app.js
...
user: null,
initialize: function () {
_.bindAll(this, 'registerSuccess', 'loginSuccess');
this.user = new userModel();
}
registerSuccess: function () {
this.user.auth({
success: this.loginSuccess
});
});
loginSuccess: function () {
console.log('loginSuccess');
}
...
, auth: function (options) {
$.ajax({
url: 'users/auth'
, type: 'post'
}, options);
})
var api = require('./routes/apiController');
app.post('/users/auth', api.userAuth);
exports.userAuth = function (req, res) {
return res.send( {
status: 200
});
}
apiController.js
...
user: null,
initialize: function () {
_.bindAll(this, 'registerSuccess', 'loginSuccess');
this.user = new userModel();
}
registerSuccess: function () {
this.user.auth({
success: this.loginSuccess
});
});
loginSuccess: function () {
console.log('loginSuccess');
}
...
, auth: function (options) {
$.ajax({
url: 'users/auth'
, type: 'post'
}, options);
})
var api = require('./routes/apiController');
app.post('/users/auth', api.userAuth);
exports.userAuth = function (req, res) {
return res.send( {
status: 200
});
}
我得到了回应:
但它不会打印此函数中的
console.log('loginsucess')
语句。有两种调用方法:
在第一种情况下,url
必须是字符串,settings
是可选对象;在第二种情况下,settings
也是可选对象。您正在使用两个对象调用$.ajax
:
auth: function (options) {
$.ajax({
url: 'users/auth'
, type: 'post'
}, options);
}
由于第一个参数不是字符串,$.ajax
将使用第二个调用表单并完全忽略选项。您可能希望使用合并参数:
$.ajax(_({
url: 'users/auth'
, type: 'post'
}).extend(options));
或者,如果要避免意外覆盖url
和类型
:
$.ajax(_({
url: 'users/auth'
, type: 'post'
}).defaults(options));
或者,由于您只有两件事,只需手动将它们填入:
options = _(options).clone(); // Avoid accidentally messing with the argument.
options.url = 'users/auth';
options.type = 'post';
$.ajax(options);
可能输入错误不在您的原始代码中,但在mainView.js中显示loginsucess;function(){
而不是loginsAccess:function(){
,它应该是一个冒号,而不是分号。不,它不是。这是一个编辑错误,抱歉。它起作用了,我必须阅读更多关于$.ajax的内容,我现在阅读jquery文档,它说:jquery.ajax(url,[settings])或jquery.ajax([settings]),其中[settings]是一个普通对象键值。非常感谢。