久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

node.js - node express 中ajax post請(qǐng)求參數(shù)接收不到?

瀏覽:135日期:2024-07-19 17:38:55

問題描述

app.js

// bodyParser :ajax 請(qǐng)求的配置項(xiàng)//parseapplication/x-www-form-urlencodedapp.use(bodyParser.urlencoded({extended: false}));//parseapplication/json:接受 json 或者可以轉(zhuǎn)換為json的數(shù)據(jù)格式app.use(bodyParser.json({type: ’application/*+json’}));// 路由routes(app);

user.js

'use strict';var express = require(’express’);var router = express.Router();// 該路由使用的中間件 timeLogrouter.use(function timeLog(req, res, next) { console.log(’Time: ’, Date.now()); next();});// 定義網(wǎng)站主頁的路由router.get(’/’, function (req, res) { // console.log(req); res.send(’user home page’);});// 定義 about 頁面的路由router.get(’/about’, function (req, res) { console.log(req.query); res.send(’About user’);});// 定義 login 頁面的路由router.post(’/login’, function (req, res) { console.log(req); //**拿不到對(duì)應(yīng)的ajax參數(shù)** res.send(’user login’);});// 定義 logout 頁面的路由router.post(’/logout’, function (req, res) { res.send(’user logout’);});module.exports = router;

page.js

$.ajax({ url: 'http://127.0.0.1:3000/user/login', type: 'post', data: {type: 'post',target: 'login' }, callback: function (data) {console.log(data); }})//data 信息Object {xhr: XMLHttpRequest, errorType: 'parsererror', error: SyntaxError: Unexpected token u in JSON at position 0 at Function.parse [as parseJSON] (<anonymo…}error:SyntaxError: Unexpected token u in JSON at position 0 at Function.parse [as parseJSON] (<anonymous>) at XMLHttpRequest.xhr.onreadystatechange (http://127.0.0.1:3000/src/common/zepto.js:1486:103)errorType:'parsererror'xhr:XMLHttpRequest__proto__:Object

問題:login的post請(qǐng)求中獲取不到相應(yīng)的入?yún)ⅰ?/b>

// 定義 login 頁面的路由router.post(’/login’, function (req, res) { console.log(req); //**拿不到對(duì)應(yīng)的ajax參數(shù)** res.send(’user login’);});

備注:我先不打算做數(shù)據(jù)庫那一塊,先把基本的業(yè)務(wù)邏輯寫一些,下周才打算連接到數(shù)據(jù)庫相關(guān)的知識(shí) 。

同時(shí)我是前端開發(fā),所以喜歡前后端分離,而不喜歡在服務(wù)端寫頁面模板,謝謝!

node.js - node express 中ajax post請(qǐng)求參數(shù)接收不到?

問題解答

回答1:

// 定義 login 頁面的路由

router.post(’/login’, function (req, res) { console.log(req); //**拿不到對(duì)應(yīng)的ajax參數(shù)** res.send(’user login’);});

req是undefined?還是req.body是undefined?

app.js文件中有沒有設(shè)置 app.use(’/user’, user);?

我是這樣做的,你參考一下。你的出現(xiàn)問題,可能是哪里設(shè)置不正確。

前端

$.ajax({ type: ’POST’, url: ’/post.html’, data, success: (data, status, xhr) => { console.log(data); }});

后端

let data = req.body;回答2:

router.post(’/user/login’, function (req, res) { console.log(req); //**拿不到對(duì)應(yīng)的ajax參數(shù)** res.send(’user login’);});

或者

app.use(’/user’,require(’./user’));回答3:

errorType: 'parsererror'

你返回的不是一個(gè) JSON

回答4:

$.ajax({url: 'http://127.0.0.1:3000/user/login',type: 'post',contentType:’application/json’,data: JSON.stringify({ type: 'post', target: 'login'}),success: function (data) { console.log(data);} }) // 如果是用jquery的話 應(yīng)該這么寫

相關(guān)文章:
主站蜘蛛池模板: 武冈市| 南漳县| 利津县| 遂川县| 土默特右旗| 绥芬河市| 静安区| 静海县| 仁怀市| 郁南县| 吉木乃县| 临汾市| 黄平县| 金堂县| 中阳县| 长武县| 五寨县| 文昌市| 灵武市| 双鸭山市| 防城港市| 大同市| 社旗县| 潼南县| 琼海市| 渝北区| 当雄县| 阿拉尔市| 正蓝旗| 鸡泽县| 榆社县| 九台市| 明星| 宽城| 渝中区| 瑞安市| 延川县| 漳州市| 连山| 五大连池市| 二连浩特市|