【问题标题】:Error: XMLHttpRequest error - Sending data from flutter app to Nodejs Server错误:XMLHttpRequest 错误 - 将数据从颤振应用程序发送到 Nodejs 服务器
【发布时间】:2020-12-20 19:42:21
【问题描述】:

我正在尝试将 JSON 格式的数据发送到我的 Nodejs 服务器并收到上述错误。 我的flutter侧代码如下:

http.Response response1 = await http.post(Provider.of<CP>(context, listen: false).getsubmitOrderURL(), body: { 'Order': Provider.of<CP>(context, listen: false).getOrder().toString() }).timeout(Duration(seconds: 10), onTimeout: (){
  ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Error communicating with the server'), duration: Duration(seconds: 2)));
  return;
});

我的服务器端代码如下:

const express = require("express");
const bodyParser= require("body-parser");
const ejs = require("ejs");
const https = require('https');

const cookieParser = require("cookie-parser");

const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname, "/public")));
app.use(bodyParser.json());
app.use(cookieParser());
app.set("view engine", "ejs");

app.get("/receiveOrder", function(req,res){
  console.log(req.body.order);
  res.sendStatus(200);
  return;
)}

产生的错误:

Error: XMLHttpRequest error.

这是我第一次在涉及服务器的情况下编写我的应用程序。请帮助找出解决方案。

【问题讨论】:

  • 在客户端或服务器端得到错误,详细说明这个错误

标签: node.js express flutter flutter-web body-parser


【解决方案1】:

所以经过几个小时的试验。我发现错误是由于 CORS 政策造成的。我在节点端添加了 app.use(Cors()) 并在此之前安装了 CORS。现在一切正常。

【讨论】:

    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多