【问题标题】:mystery request proc_name= &params=神秘请求 proc_name= &params=
【发布时间】:2018-07-19 18:40:00
【问题描述】:

尝试调试一个新的 React-native 应用程序 - 非常大而且它没有后端,所以我正在尝试存根一些东西。

请求格式对我来说很陌生。有谁认识这种格式或知道如何处理它?

proc_name=customer_ref&params={"fields" : ["customerId"],"conditions":[{"mailAddress":"dsadsa"},{"pinCode":"ZHNhZHNh"},{"mailReceiveFlag":"1"}],"order" : ["customerCode desc"],"limit" : "1","table_name" : "Customer"}

React-native 请求代码如下所示:

try{
    let result = await fetch(apiUrl, {
        method: 'POST',
        headers: {
          'X_contract_id': contact_id,
          'X_access_token': access_token,
          'Content-Type': 'application/x-www-form-urlencoded',
          'charset':'UTF-8',
        },
        body: 'proc_name='+proc_name+'&params={'+params+'}'
    }).then((response) => response.json())
    .then((responseJson) => {
      return responseJson.result;
    })
    return result;
}catch(e){
    console.log(e);
}

应用程序在到达我启动的服务器之前就出现了未处理的 Promise Rejection 错误。这里很新;我在前端层做错了吗?

【问题讨论】:

  • 参数看起来更像一个 JSON 字符串。如果 Content 类型是 application/json,我会推荐 JSON.stringify(params)。

标签: http react-native


【解决方案1】:

尝试将您的 fetch 结构更改为这样。

async functionName(){

try{
    let result = await fetch(apiUrl, {
        method: 'POST',
        headers: {
          'X_contract_id': contact_id,
          'X_access_token': access_token,
          'Content-Type': 'application/x-www-form-urlencoded',
          'charset':'UTF-8',
        },
        body: 'proc_name='+proc_name+'&params={'+params+'}'
    });
    let resultjson = await result.json();
    return resultjson.result;
}catch(e){
    console.log(e);
}

}

https://facebook.github.io/react-native/docs/network.html

【讨论】:

  • 因为 fetch 是异步的,所以你不能使用 try-catch 语句来处理异步抛出的异常。如果您需要其他信息,请查看此处stackoverflow.com/questions/24977516/…
  • 我看到同样的错误,无论是在总体 try 语句中捕获,还是通过 catch 处理程序附加到承诺
  • 顺便说一句,这段代码是否放在异步函数中?例如:异步 testfunction(){...}
  • 啊,是的,对不起,DX 漏掉了
  • 也尝试根据文档更改您的代码,我刚刚更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 2021-12-14
  • 1970-01-01
  • 2021-12-31
相关资源
最近更新 更多