Axios可以获取但不能发布到同一URL [英] Axios can GET but not POST to the same URL

查看:87
本文介绍了Axios可以获取但不能发布到同一URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我正在构建一个React应用
  • 在一个组件中,我正在编写此有效的GET请求:

  • 在另一个组件中,我正在编写此POST请求:

  • 然后返回以下404错误:

我不知道我的GET如何工作,但是我两次都请求同一个文件时POST返回404:找不到?

And I have no idea how my GET works but my POST returns 404:not found when I'm requesting the same file both times?

更新:

我现在正在运行一个node.js服务器,但这有点像科学怪人的怪物,因为这确实不是我了解的领域.有人知道我在做什么错吗?

I'm running a node.js server now but it's a bit of a frankenstein's monster as this really isn't an area I have an understanding of. Does anyone know what I'm doing wrong?

// Server setup from node.js website
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});


// Trying to listen for data from React app to feed into JSON (broken)
var express = require("express");
var myParser = require("body-parser");
var app = express();

app.use(myParser.urlencoded({extended : true}));
app.post("/scene-setup.json", function(request, response) {
    console.log(request.body); //This prints the JSON document received (if it is a JSON document)
});
app.listen(3001);


// Updating JSON file with "obj" (working)
var jsonfile = require('jsonfile')

var file = './scene-setup.json'
var obj = {name: 'JP'}

jsonfile.writeFile(file, obj, function (err) {
  console.error(err)
})

推荐答案

Axios用于发出HTTP请求.因此,您应该运行一个可以处理这些请求的后端服务器.我不确定您要保存的数据到底是什么.如果您需要访问该数据,应将其保存在后端.

Axios is used for making HTTP requests. So, you should have a backend server running that can handle these requests. I am not sure what exactly is the data that you want to save. If you need access to that data, should be saving it on the backend.

如果您只想在客户端保存一些数据,请

If you want to save some data just on the client side, HTML5 filesystem API might be something you want to look at. It can manage some data in the limited sandboxed part of user's filesystem.

这篇关于Axios可以获取但不能发布到同一URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆