使用 Node.js 获取 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)"

时间:2020-12-30 09:24:30

标签: html node.js json express

const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const https = require("https") ;

const app = express();

// keep static files (e.g css,image files) inside a folder called 'Public'
app.use(express.static("public"));

app.use(bodyParser.urlencoded({extended: true})) ;

app.get("/", (req, res) => {
    res.sendFile(__dirname + "/app.html") ;
});

app.post( "/", (req, res) => {
    const firstName = req.body.fName ;
    const lastName = req.body.lName ;
    const email = req.body.eMail ;
    let data = {
        members: [
            {
                email_address: email,
                status: "subscribed",
                merge_fields: {
                    FNAME: firstName,
                    LNAME:lastName
                }
            }
        ]
    }

    const jsonData = JSON.stringify(data);
    const url = "https://us7.api.mailchimp.com/3.0/lists/0457e81530";
    const options = {
        method: "POST" ,
        auth: "Jewel:a940bdffbeedd3146d8722711e7c4e98-us7"
    }
    const request = https.request(url, options, (response) => {
        response.on("data", (data) => {
            console.log(JSON.parse(data));
        })
    });
    request.write(jsonData);
    request.end;
});

app.listen( process.env.PORT || 3000, function () {
    console.log("server is running on port 3000") ;
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./public/style.css">
    <title>Document</title>
</head>
<body>
    <form action="/" method="POST">
        <p> <input name="fName" type="text" placeholder="First Name" required> </p>
        <p> <input name="lName" type="text" placeholder="Last Name" required> </p>
        <p> <input name="eMail" type="email" placeholder="email" required> </p>
        <p><button type="submit"> SUBMIT </button></p>

    </form>
    <script src="app.js"></script>
</body>
</html>

以下是我在 CLI 中的错误 SyntaxError: Unexpected token < in JSON at position 0

server is running on port 3000
undefined:1
<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>
^

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (C:\Users\Jewel\Documents\HMDT ADF\getting started\PR0JECT_1\nodejs\form\app.js:42:34)
    at IncomingMessage.emit (events.js:315:20)
    at IncomingMessage.Readable.read (_stream_readable.js:519:10)
    at flow (_stream_readable.js:992:34)
    at resume_ (_stream_readable.js:973:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)

2 个答案:

答案 0 :(得分:0)

如果您想继续使用 https 模块:

const request = https.request(url, options, (response) => {
  let data = '';
  response.on("data", (chunk) => {
    data += chunk;
  });
  response.on("end", () => {
    const result = JSON.parse(data);
    console.log(result);
    res.send(result);
  });
});

答案 1 :(得分:0)

你的网址是错误的。 使用邮递员应用并发布该网址,您将看到返回错误页面。

我相信你最后缺少的“/members”。 添加到您的选项“正文:数据” 并且您的数据不应该是数组,而应该是对象(这是我在处理上述 2 条建议后发现的错误)。