使用axios向Web服务器发送请求。

huangapple go评论82阅读模式
英文:

Sending request to webserver using axios

问题

I understand you want the code part to be translated. Here is the translated code:

客户端代码:

const axios = require('axios');
let listOfNames = ['mikey'];
axios.post('http://localhost:3000/start', {
    data: { names: listOfNames }
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});

服务器端代码:

const express = require('express');
const app = express();
const port = 3000;
var listOfNames = [];
app.post('/start', async (req, res) => {
    listOfNames = req.body.data.names;
    res.status(200).send("Names added");
});

app.listen(port, () => {
    console.log('request received');
});

The code has been translated into Chinese as requested.

英文:

I want to send an array of strings over localhost 3000 with route start then send back a response with status 200 and eventually a map attached to response.body Currently i have this
Client code:

const axios = require('axios');
let listOfNames = ['mikey'];
axios.post(''http://localhost:3000/start'', {
        data: { names: listOfNames }
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

Server code:

const express = require('express');
const app = express()
const port = 3000
var listOfNames = [];
app.post('/start', async (req, res) => {
  listOfNames = req.params.listOfNames;
  res.status(200).send("Names added");
});

app.listen(port, () => {
  console.log('request recieved');
});

I get this error presemably from how the request is being sent, any help?

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:393:5)
    at URL.onParseError (node:internal/url:565:9)
    at new URL (node:internal/url:645:5)
    at dispatchHttpRequest (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:23
94:20)
    at new Promise (<anonymous>)
    at http (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:2330:10)
    at Axios.dispatchRequest (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:
3260:10)
    at Axios.request (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:3610:33)
    at Axios.httpMethod [as post] (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios
.cjs:3649:19)
    at Function.wrap [as post] (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cj
s:27:15) {
  input: '/start',
  code: 'ERR_INVALID_URL'
}

Edit: New error ECONNRESET error emerging from applied fixes

AxiosError: read ECONNRESET
    at AxiosError.from (C:\Users\cmb\rectangleHealth\node_modules\axios\dist\node\axios.cjs:789:14
)
    at RedirectableRequest.handleRequestError (C:\Users\cmb\rectangleHealth\node_modules\axios\dis
t\node\axios.cjs:2744:25)
    at RedirectableRequest.emit (node:events:513:28)
    at eventHandlers.<computed> (C:\Users\cmb\rectangleHealth\node_modules\follow-redirects\index.
js:14:24)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:494:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  syscall: 'read',
  code: 'ECONNRESET',
  errno: -4077,

The console also outputs a 2 json objects called request and config that cannot fit into this post.

答案1

得分: 1

I noticed 2 things errors in your code:

First, check your url is correct, instead of

"http://localhost:3000/start" (you have multiple single quotes wrapping the url)

try,

"http://localhost:3000/start" or 'http://localhost:3000/start' (wrap it in proper double quotes or single quotes)

Second, You are passing the data in your api call as request body and not as request parameters, but you are trying to access it in the parameters of your api.

You should try accessing the request's body on the server side instead of parameters,

app.post('/start', async (req, res) => {
  listOfNames = req.body.listOfNames;
  res.status(200).send("Names added");
});

Else you might probably face issue while accessing the data in api as well.

英文:

I noticed 2 things errors in your code:

First, check your url is correct, instead of

''http://localhost:3000/start'' (you have multiple single quotes wrapping the url)

try,

"http://localhost:3000/start" or 'http://localhost:3000/start' (wrap it in proper double quotes or single quotes)

Second, You are passing the data in your api call as request body and not as request parameters, but you are trying to access it in the parameters of your api.

You should try accessing the request's body on the server side instead of parameters,

app.post('/start', async (req, res) => {
  listOfNames = req.body.listOfNames;
  res.status(200).send("Names added");
});

Else you might probably face issue while accessing the data in api as well.

huangapple
  • 本文由 发表于 2023年1月6日 12:21:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定