简体   繁体   English

如何使用Ajax将用户名和密码发送到快递服务器?

[英]How can I send username and password to an express server with ajax?

This is the ajax method to send the form to server: 这是将表单发送到服务器的ajax方法:

$("#submit").click( function() {

    $.ajax({
        url: '/login',
        type: "POST",
        dataType: "html",
        contentType: 'application/x-www-form-urlencoded',
        data: $('#submit-form').serialize(),
        username: $('#username').val(),
        password: $('#password').val(),
        beforeSend: function(xhr) {},
        success: function(data) {               
             console.log('Success');
        },
        error: function(jqXHR, textStatus, errorThrown) {
             // alert('error ' + textStatus + " " + errorThrown);
             console.log(this.data);
             console.log('Error', textStatus, errorThrown);
        }
    });

});

The following is my index.js: 以下是我的index.js:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/login', function (req, res) {
    console.log('body=', req.body);
    res.end('{"success" : "Success", "status" : 200}');
});

But i'm only getting 'Error' and sometimes the server gets the POST and sometimes not, why and how can i fix this? 但是我只收到“错误”,有时服务器收到POST,有时没有,我为什么以及如何解决这个问题?

$(this).serialize() should be referencing your form (eg, $('#my-form').serialize() ). $(this).serialize()应该引用您的表单(例如$('#my-form').serialize() )。

from https://api.jquery.com/serialize/ 来自https://api.jquery.com/serialize/

It can act on a jQuery object that has selected individual form controls, such as <input> , <textarea> , and <select> 它可以作用于已选择单个表单控件的jQuery对象,例如<input><textarea><select>

if that is indeed your form and you still have issues, what is the value of textStatus and errorThrown in the error callback? 如果那确实是您的表单并且仍然存在问题,那么错误回调中的textStatuserrorThrown的值是什么? does the request reach the console.log('Login request') statement? 请求是否到达console.log('Login request')语句?

the following express code works for me: 以下快速代码对我有用:

...
var app = express()
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))

app.post('/login', function (req, res) {
  console.log('body=', req.body)
})
...

with the following AJAX code: 使用以下AJAX代码:

$.ajax({
  url: '/login',
  type: "POST",
  dataType: "html",
  data: $('#form').serialize(),
  contentType: 'application/x-www-form-urlencoded',
  username: $('#username').val(),
  password: $('#password').val(),
  beforeSend: function(xhr) {},
  success: function(data) {               
      console.log('Success');
  },
  error: function(jqXHR, textStatus, errorThrown) {
      // alert('error ' + textStatus + " " + errorThrown);
      console.log('Error', textStatus, errorThrown)
  }
})

EDIT added sample express & ajax code 编辑添加了示例Express和Ajax代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何将用户名和密码发送到服务器? - How should I send the username and a password to the server? 如何使用 axios 请求发送用户名和密码 - How can I send username and password with axios request 使用ajax发送用户名和密码是否安全? - Is it safe to send username and password using ajax? 如何在不键入的情况下发送用户名和密码? - How can you send Username and Password without typing it? 如何不使用POST或GET输入用户名和密码输出到MySQL服务器 - How Can I Enter Username And Password Outputs To A MySQL Server Without Using POST or GET 如何使用Express ntlm实现用户名/密码身份验证? - How to implement username/password auth with express ntlm? 我如何在不使用Ajax的情况下将对象发送到表单提交的服务器上? - How can I send an object to the server on form submit without ajax? 如何使用 AJAX 将 JSON 数据发送到 ASP 服务器? - How can I send JSON data to ASP Server using AJAX? 如何在服务器端检索文件,使用 ajax 为节点/快递应用程序发送 - How to retrieve file on server side send with ajax for a node/express app 如何从 FormData() 访问我的数据(用户名、密码等;)到我的快速服务器 - How to get access my data (username, password etc;) from FormData() into my express server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM