superagent post vs 投入反应

superagent post vs put in react

var request = require('superagent');
var url = 'api/server';
request.put(url)
    .set('Content-Type', 'application/json')
    .send('{"name":"tj","pet":"tobi"}')
    .end(function(err, res){
        if (err) throw err;
        console.log(res.text);
    });

以上是我用于上传数据的代码。如果我将 put 更改为 post,它就不起作用了。我不知道这是为什么,有人可以帮忙吗?

另外,其实我想上传一个文件。但是我不能将 put 与 .attach('theFile', file) 一起使用。我搜索了很多示例,但其中 none 对我有用。

可能您的服务器需要 api/server url 上的 PUT 而不是 POST 方法,请检查服务器源代码或文档。

处理您的请求的服务器读取路由 (api/server) 和方法以确定对 return 的响应。如果未配置为接受对该特定路由的 POST 请求,请求将失败。

此外,superagent 不允许您在同一请求中使用 attachsend。而不是 send 使用 field:

.field('name', 'tj')
.field('pet', 'tobi')

阅读 the docs 了解更多详情。