简体   繁体   English

axios不在nodejs中发送请求正文

[英]axios not sending request body in nodejs

I'm trying to send a POST request using axios from NodeJS, but the body/data of the request is not sent.我正在尝试使用来自 NodeJS 的 axios 发送 POST 请求,但未发送请求的正文/数据。

I am using axios and NodeJS on the back end, and this is my NodeJS script which sends the request:我在后端使用 axios 和 NodeJS,这是我发送请求的 NodeJS 脚本:

const axios = require("axios").default;

axios.post("my url goes here/email.php", {
   emailto: "receiver email",
    toname: "name",
    emailfrom: "sender email",
    fromname: "name",
    subject: "subject",
    messagebody: "hello world"
});

When the receiving server tries to read the data in the request (eg by declaring $subject = $_POST[subject]; in PHP), the parameters of the request are null - so $_POST[subject] returns null .当接收服务器尝试读取请求中的数据时(例如,通过声明 $subject = $_POST[subject]; 在 PHP 中),请求的参数是null - 所以$_POST[subject]返回null

However, when I send the same request using not axios but jQuery (on the front end), it works perfectly in that the receiving server can now read the data of the POST request (so $_POST[subject] this time returns "subject"):但是,当我使用不是 axios 而是 jQuery (在前端)发送相同的请求时,它工作得很好,因为接收服务器现在可以读取 POST 请求的数据(所以$_POST[subject]这次返回“主题” ):

$.ajax("https://my url goes here/email.php", {
  method: "POST",
  cache: false,
  data: {
        emailto: "receiver's email",
        toname: "name",
        emailfrom: "sender email",
        fromname: "name",
        subject: "subject",
        messagebody: "hello world"
    }
})

When I run the second snippet, on the PHP-server $_POST[subject] returns "subject", but when I run the first snippet in NodeJS, $_POST[subject] returns null .当我运行第二个片段时,在 PHP 服务器上$_POST[subject]返回“subject”,但是当我在 NodeJS 中运行第一个片段时,$_POST $_POST[subject]返回null Why is axios not sending my request body?为什么 axios 没有发送我的请求正文?

Look at the documentation for axios :查看axios 的文档

By default, axios serializes JavaScript objects to JSON默认情况下,axios 将 JavaScript 对象序列化为 JSON

So you are POSTing JSON to the server.因此,您将 JSON 发布到服务器。

PHP doesn't support JSON when it comes to populating $_POST . PHP 在填充$_POST时不支持 JSON 。

You need to POST a format that it supports, such as URL Encoded Form Data or Multipart Form Data.您需要 POST 它支持的格式,例如 URL 编码表单数据或多部分表单数据。

(Your jQuery example is using URL Encoded Form Data). (您的 jQuery 示例使用 URL 编码表单数据)。

To send data in the application/x-www-form-urlencoded format instead, you can use the URLSearchParams API, which is supported in the vast majority of browsers, and Node starting with v10 (released in 2018).要改为以 application/x-www-form-urlencoded 格式发送数据,您可以使用绝大多数浏览器都支持的 URLSearchParams API,以及从 v10(2018 年发布)开始的 Node。

 const params = new URLSearchParams({ foo: 'bar' }); params.append('extraparam', 'value'); axios.post('/foo', params);

add a content body such as { headers: {'Content-Type': 'application/json'} to the request but I would edit this and向请求添加内容主体,例如{ headers: {'Content-Type': 'application/json'}但我会编辑它并

const axios = require("axios");

var data = JSON.stringify({ 
        emailto: "receiver email",
        toname: "name",
        emailfrom: "sender email",
        fromname: "name",
        subject: "subject",
        messagebody: "hello world"
        });

var config = {
  method: 'post',
  url: 'https://alpha-howl.com/database/email.php',
  headers: { 
    'Content-Type': 'application/json'
  },
  data : data
};


try { 
    await axios(config)
} catch (ex) { 
throw ex
} 

Mainly to make sure you are resolving the promise that is returned by axios to make sure the request is even successful.主要是为了确保您正在解析 axios 返回的 promise 以确保请求甚至成功。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM