代码之家  ›  专栏  ›  技术社区  ›  Tehreem

API在邮递员中返回200,但在设备中返回404

  •  0
  • Tehreem  · 技术社区  · 6 年前

    我使用koa服务器和postgresql数据库为我的应用程序创建了一个简单的api,并使用postman和浏览器对其进行了测试,两者都运行良好。现在,我正试图在我的react原生应用程序中获取数据(在实际设备上测试),但它向我返回了以下响应:

    {"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "1bbc1ba3-a1b4-442e-ba85-d09d5d54f4e6", "offset": 0, "size": 9}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "1bbc1ba3-a1b4-442e-ba85-d09d5d54f4e6", "offset": 0, "size": 9}}, "headers": {"map": {"content-length": "9", "content-type": "text/plain; charset=utf-8", "date":
    "Mon, 20 Apr 2020 12:26:12 GMT"}}, "ok": false, "status": 404, "statusText": undefined, "type": "default", "url":
    "https://a1cd97c8.ngrok.io/posts/,function%20dispatchAction()%20%7B%20%20%20%20[native%20code]%7D"}
    

    我使用ngrok URL映射,因为没有https,我的请求会被直接拒绝。

    这是我的请求代码:

    const searchURL = React.useState('https://a1cd97c8.ngrok.io/posts/');    
    const fetchPosts = async () => {
            console.log('fetching');
            fetch(searchURL, {
              method: 'GET',
              headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
              },
            })
              .then((response) => {
                console.log('response: ', response);
                return response.json();
              })
              .then((json) => console.log('TESTOUTPUT', json))
              .catch((err) => console.log('error: ', err.message));
          };
    

    我在catch块中收到的错误消息是:

    error:  JSON Parse error: Unexpected identifier "Not"
    

    我做错了什么?

    0 回复  |  直到 6 年前
        1
  •  2
  •   Mahdi N    6 年前

    useState 钩子返回一个数组,所以你应该解构结果

    const [searchURL, setSearchURL] = React.useState('https://a1cd97c8.ngrok.io/posts/');