首页 > 解决方案 > 使用 React 发出 HTTP POST 请求

问题描述

你如何从以下 React 发出 POST 请求:

http --auth name:password POST http://localhost:5000/api/tokens

我目前使用 React 提出请求的尝试如下:

const requestOptions = {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        name: 'name',
        password: 'password'
    })
};
fetch('/api/tokens', requestOptions).then(res => res.json());

谢谢

标签: javascriptreactjsresthttp

解决方案


推荐阅读