简体   繁体   English

如何使用提取以应用程序/ JSON的内容类型发布

[英]How to use fetch to post with content-type of application/json

I have a react application that does a fetch call to an API as follows: 我有一个react应用程序,它对API进行提取调用,如下所示:

 postForm(state) {
    var formData = state.formData;
    return fetch('http://localhost:3000/api/V0.1/formSubmit', {method: 'POST', headers: {"Content-Type": "application/json"}, body: JSON.stringify(formData)})
    .then((response) => response.json())
    .then((responseJson) => {
      console.log(responseJson);
      return null;
    })
    .catch((error) => {
      console.error(error);
    });
  }

however, it gets blocked by CORS as the spec states that application/json is non-standard content type. 但是,由于规范指出application / json是非标准的内容类型,因此它被CORS阻止。

However, I'm not sure how I can modify my fetch call to perform the required pre-flight request to get it to allow application/json. 但是,我不确定如何修改获取请求以执行所需的飞行前请求,以使其允许应用程序/ json。

The API call is: API调用为:

app.post("/api/v0.1/formSubmit", function(req, res) {
  res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.setHeader('Access-Control-Allow-Headers', '*');
  var formData=req.body;
  console.log(formData);
  res.status(200).json(formData);
});

Before defining your routing. 在定义您的路由之前。 declare 宣布

app.use(function(req, res, next) {
        var oneof = false;
        if(req.headers.origin) {
            res.header('Access-Control-Allow-Origin', req.headers.origin);
            oneof = true;
        }
        if(req.headers['access-control-request-method']) {
            res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
            oneof = true;
        }
        if(req.headers['access-control-request-headers']) {
            res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
            oneof = true;
        }
        if(oneof) {
            res.header('Access-Control-Max-Age', 60 * 60 * 24 * 365);
        }

        // intercept OPTIONS method
        if (oneof && req.method == 'OPTIONS') {
            res.send(200);
        }
        else {
            next();
        }
    });

In CORS, the request is checked for available methods on server. 在CORS中,将检查请求中服务器上的可用方法。 ie in OPTIONS request. 即在OPTIONS请求中。 When you get the successful response, you will able to send request. 收到成功的响应后,便可以发送请求。

You can enable CORS for specific pages also. 您也可以为特定页面启用CORS。 study here https://github.com/expressjs/cors 在这里学习https://github.com/expressjs/cors

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

相关问题 错误:使用'application / json'Content-Type和带有json数据的原始POST - error : use 'application/json' Content-Type and raw POST with json data 如何将JSON和图像发布到WCF(使用内容类型:application / JSON) - How to Post a JSON and an Image to WCF (Using Content-type: application/JSON) Camunda:具有Content-Type application / json的Http连接器发布方法 - Camunda:Http Connector Post Method With Content-Type application/json 无法发布内容类型:application / json从角度到导轨 - Cannot POST with content-type: application/json from angular to rails 内容类型application / json不返回POST字段 - Content-Type application/json doesn't return POST fields Sails js http post 请求,内容类型为:application/json - Sails js http post request with content-type: application/json 烧瓶中没有POST请求和Content-Type“application / json”的响应 - No response with POST request and Content-Type “application/json” in flask 如何将'content-type application / json'的数据发布到苗条的php rest服务 - How to post data of 'content-type application/json' to slim php rest service 如何在C#4.0中读取内容类型为application / json的HTTP Post数据 - How to read HTTP Post data with content-type of application/json in C# 4.0 如何在此请求中添加Content-Type application / json - How to add Content-Type application/json in this request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM