Jquery 如何在验证后以异步方式返回值?
在验证之后,我花了几个小时试图从函数中获取值,但它似乎不起作用。我试过看类似的线索,但仍然无法理解它们的答案。这是我的密码Jquery 如何在验证后以异步方式返回值?,jquery,html,ajax,Jquery,Html,Ajax,在验证之后,我花了几个小时试图从函数中获取值,但它似乎不起作用。我试过看类似的线索,但仍然无法理解它们的答案。这是我的密码 /*$("#submit").click(function(){ // Set form variable var form = $('#loginform'); form.submit(function(event){ var userid = $('#userID').trim(us
/*$("#submit").click(function(){
// Set form variable
var form = $('#loginform');
form.submit(function(event){
var userid = $('#userID').trim(userid);
var password = $('#Password').trim(password);
// Process AJAX request
var validate = $.post('http://localhost:3000/api/login',
$("loginform").serialize(), function(data){
alert(data);
});
// Prevent default form action
event.preventDefault();
});
}); */
function ajaxcall(url, data, callback) {
$.ajax({
"url": "http://localhost:3000/api/login", // server url
"type": 'POST', //POST or GET
"data": "#loginform", // data to send in ajax format or querystring format
"datatype": 'json',
"beforeSend": function() {
alert('sending data');
// do some loading options
},
success: function(data) {
if(data=="Login Success"){
callback(true); // return data in callback
} else {
callback(false);
}},
complete: function() {
alert('ajax call complete');
// success alerts
},
error: function(xhr, status, error) {
alert(xhr.responseText); // error occur
}
});
}
注释的第一部分基本上是我尝试过的,也失败了,所以我决定改用$.ajax
,但似乎没有什么变化。这是我的html代码
<form method="POST" action="index.html" id="loginform" onsubmit="return ajaxcall(callback)" >
我正在寻找一些代码作为参考或简单的解释等。我以前看过,并尝试过,但它不起作用,所以我猜它与我的代码本身,而不是html。请原谅我问了同样的问题,但在阅读了这篇文章几个小时试图理解之后,我几乎被卡住了
服务器代码:
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
var authenticate = function(db, req, callback){
var cursor = db.collection('LoginID').find({"_id" : req.body.userID,
"Password" : req.body.Password
}).count(function(err,doc){
if(err) return callback(err);
if(doc == 0){
console.log('Invalid Login ID or Password');
return callback(null, doc);
} else {
console.log('Login Success');
return callback(null, doc);
}
});
}
module.exports = {
postCollection : function(req,res){
var username = req.body.userID;
var Password = req.body.Password;
//var createdDate = "<b>" + day + "/" + month + "/" + year + "</b>"
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if(err) {
res.send(err);
res.end();
}
authenticate(db, req, function(err,doc) {
if(err)
res.send(err);
else{
if(!doc){
res.send( 'Invalid Login ID or Password' );
res.end();
} else {
res.send("Login success")
res.end();
}
}
db.close();
});
});
}
}
var MongoClient=require('mongodb')。MongoClient;
var url='1〕mongodb://localhost:27017/myproject';
var authenticate=函数(db、req、回调){
var cursor=db.collection('LoginID').find({“\u id”:req.body.userID,
“密码”:req.body.Password
}).count(函数(错误、单据){
if(err)返回回调(err);
如果(doc==0){
console.log('无效的登录ID或密码');
返回回调(空,单据);
}否则{
log('Login Success');
返回回调(空,单据);
}
});
}
module.exports={
收集后:功能(请求、恢复){
var username=req.body.userID;
var Password=req.body.Password;
//var createdDate=“+day+”/“+month+”/“+year+”“
连接(url,函数(err,db){
//assert.equal(null,err);
如果(错误){
res.send(err);
res.end();
}
验证(数据库、请求、功能(错误、文档){
如果(错误)
res.send(err);
否则{
如果(!doc){
res.send('无效的登录ID或密码');
res.end();
}否则{
res.send(“登录成功”)
res.end();
}
}
db.close();
});
});
}
}
PS:这是为了调用我的api,然后检查数据库中是否有用户条目,如果没有,它会输出一个响应“无效的登录ID或密码”,并保持在同一页面上,但如果有用户条目,它会输出“登录成功”,然后重定向到
index.html
。如果我不清楚,很抱歉。我在表单提交方面也遇到类似问题。然后找到这个
看看这个插件
注意:尝试从服务器返回
code
而不是字符串,或者将两者解析为JSON。这通常是最佳做法。您必须将键值对传递给数据
属性
“对象必须是键/值对。”
因此,您必须将数据更改为以下内容:
data: {loginform_id: "#loginform"},
您还必须从ajax属性中删除“
”
e、 g.改变这一点:
"url": "http://localhost:3000/api/login", // server url
"type": 'POST', //POST or GET
"data": "#loginform", // data to send in ajax format or querystring format
"datatype": 'json',
"beforeSend": function() {
alert('sending data');
// do some loading options
},
为此:
url: "http://localhost:3000/api/login", // server url
type: 'POST', //POST or GET
data: {loginform_id: "#loginform"}, // data to send in ajax format or querystring format
datatype: 'json',
beforeSend: function() {
alert('sending data');
// do some loading options
},
请张贴您的相关服务器code@Black用服务器代码编辑它该线程超级简短地说,你绝对不能从和异步函数返回值,实际上你不应该。基本上,你应该在定义的同一个回调中继续使用该值。试试谷歌搜索“javascript什么是异步函数“
onsubmit=“return ajaxcall(回调)“
因此,从我在代码中看到的情况来看,回调
是未定义的。。更多关于错误是什么的信息?@KirillRogovoy这是错误的,当然你可以从异步调用中接收数据,你将在success
中定义的函数中得到响应,并检查你的服务器代码,因为你的函数“callback”我的服务器代码在postman上正常工作,所以我想知道哪一个是未定义的?你是指我的服务器代码还是js代码?两者似乎都未定义。或者你没有发布完整的代码?在js代码中,我认为callback(true)
已经定义,callback(false)
如果服务器代码的用户名或密码错误,我没有对res.send()使用回调
在服务器代码中,那么这是否会导致问题?在使用它之前,首先必须定义函数回调
。如果执行console.log(回调),您会得到什么输出代码>?