【问题标题】:node jquery ajax servie side can not get datanode jquery ajax 服务端无法获取数据
【发布时间】:2017-05-18 23:14:03
【问题描述】:

我是中国人,所以我的英语很差,我提供两个图片来说明我的问题 enter image description here

enter image description here

如果我去 localhost:8000/get 并且页面是空的。req.body.length 是空的,为什么?

【问题讨论】:

  • 请在您的问题中发布实际代码。
  • 两张图包含我的代码还是我没看懂你的意思?
  • 最好不要在本网站上查看您的代码。一般来说,所有问题都应该在问题中包含相关代码。
  • 您正在访问图片中的request.query.length。尝试发送/get?length=231231&height=5235345 之类的数据,然后您将在request.query 下看到它们
  • 我知道我应该发送数据。但是我已经通过 ajax 在 index.html 中发送数据。但是我在 request.query 中看不到任何内容。但是如果我通过 from 提交。我可以看到在 request.query 下,为什么?

标签: javascript node.js ajax


【解决方案1】:

另外,这里是使用 GET 的工作代码。

var express = require('express');

var fs = require('fs');
var path = require('path');

var app = express();


app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res) {
    fs.readFile('index.html', function(err, rs) {
        if (err) {
            console.log(err)
        } else {
            console.log(rs);
        }
    });

    res.render('index.html');
});
app.get('/get', function(req, res) {
    console.log(req.query.length)
    res.send("Hello from Get");
});

app.listen('8800', function(err) {
    console.log('Listening on 8800')
})
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
    <!--[if lt IE 7]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->

    Hello
    <script>
        $(document).ready(function() {
            $.ajax({
                url: '/get',
                type: 'GET',
                data: {
                    length: '12',
                    height: '122323'
                },
                success: function() {
                    console.log("success")
                }
            })
        })
    </script>
</body>

</html>

您正在 $.ajax 中使用 Get 方法。如果要在 ajax 调用体中发送数据,则需要使用 Post 方法(类型:'POST')。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 2020-09-26
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多