【问题标题】:Request works in POSTMAN but not by FETCH请求在 POSTMAN 中有效,但在 FETCH 中无效
【发布时间】:2018-07-14 23:31:22
【问题描述】:

我在试图弄清楚为什么我的请求对邮递员有效但在应用程序中无效(使用 fetchjs)时遇到了一些麻烦。 以下是我对邮递员的要求:

以下是我在节点应用中使用 fetchjs 的代码:

fetchData() {
    let body = {}
    let method = "read";
    let id = "ctzeN5gn5fkQqj9uc";
    let vals = [method,{},id];
    let headers={
        "Accept": "application/json",
        "Content-Type": "application/json",
    };

    fetch('http://localhost:8000/v1/en/reviews', {
        method: 'POST',
        headers:headers,
        dataType:'json',
        body: JSON.stringify({vals:vals})
      }).then(res => res.json())
        .then(res => {
            console.log(res);
            if (res.error) throw (res.reason);
            this.setState({ data: res.data });
        })
        .catch(res => console.error(res))
}

以下是我的快递服务器代码:

let vals = this.req.body.vals;
if (!vals) throw new Error('Vals Missing');
let vals = JSON.parse(vals);
let method = vals[0];          //>>>error here (Unexpected token r)
let args = vals[1];
let id = vals[2];

我收到以下回复:

SyntaxError: Unexpected token r"

r 实际上是方法的一部分(读取)。

知道我做错了什么吗?任何建议都非常感谢。

【问题讨论】:

  • 浏览器控制台有什么错误吗?这可能是 CORS 问题。
  • 您好,我在上面更新了,实际上不是 cors 问题,我已经在 express 服务器中使用 app.user(cors())。
  • 错误在哪一行?
  • 检查 this.req.body.vals 的数据类型实际上是字符串而不是对象 - express 可能已经将 JSON 字符串解析为对象,因为内容类型是 "application/json"。如果是这种情况,那么您将尝试解析已解析的对象并得到该错误。
  • 只需检查数据类型——如果是字符串则解析它,如果是对象则保持原样并让它通过。

标签: node.js fetch postman


【解决方案1】:

在您的选项中使用mode: 'cors'

fetch("http://localhost:8000/v1/en/reviews", { mode: "cors", method: "POST", headers: headers, dataType: "json", body: JSON.stringify({vals: vals}) }) .then((res) => res.json()) .then((res) => { console.log(res); if (res.error) throw res.reason; this.setState({data: res.data}); }) .catch((res) => console.error(res));

【讨论】:

    【解决方案2】:
    fetchData() {
        let body = {}
        let method = "read";
        let id = "ctzeN5gn5fkQqj9uc";
        let vals = [method,{},id];
        let headers= {'Content-Type': 'application/x-www-form-urlencoded'};
    
        fetch('http://localhost:8000/v1/en/reviews', {
            method: 'POST',
            headers:headers,
            body: JSON.stringify({vals:vals})
          }).then(res => res.json())
            .then(res => {
                console.log(res);
                if (res.error) throw (res.reason);
                this.setState({ data: res.data });
            })
            .catch(res => console.error(res))
    }
    

    还有

    let vals = this.req.body.vals;
    if (!vals) throw new Error('Vals Missing');
    vals = JSON.parse(vals);
    let method = vals[0];    
    

    【讨论】:

    • 感谢您的回复!尝试通过将标头内容类型更改为应用程序来做同样的事情,但我收到错误为 vals not found ,即 req.body.vals 不存在(未定义)但对于邮递员仍然适用(具有相同的标头)。我已经加了app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json());你有什么建议吗?
    • 你能告诉我 this.req.body.vals; , 为什么这个 。可以加代码吗?
    • 啊,你可以忽略这个,我实际上是在创建一个类并设置默认参数constructor(req,res){ this.req = req; this.res = res; }所以this.req.body.vals只是指当前的req。
    【解决方案3】:

    在第 5 行:let vals = [method,vals,id]; 有两个 'vals'?

    【讨论】:

      猜你喜欢
      • 2019-11-28
      • 2017-05-03
      • 2022-10-31
      • 2017-09-14
      • 2021-03-24
      • 2021-11-02
      • 1970-01-01
      • 2019-11-11
      • 2018-10-27
      相关资源
      最近更新 更多