首页 > 解决方案 > RESTful API 请求

问题描述

首先,我是巴西人,只是提醒您注意我可能犯的任何英语错误

我正在尝试通过 HTTP 为 RESTful API 发送请求,但我没有收到任何响应

<html>

<head>
    <title>BUSAO</title>
    <button type="submit" onclick="get()">GET</button>
    <button type="submit" onclick="post()">POST</button>
    <script type="text/javascript" language="javascript">
        function get() {
            const userAction = async() => {
                const response = await fetch('http://scristovao.azurewebsites.net/api/LastPositions', {
                    method: 'GET',
                    headers: {
                        'Authorization': 'Bearer <>',
                        'Access-Control-Allow-Origin': '*'
                    }
                });
                const myJson = await response.json(); //extract JSON from the http response
                // do something with myJson
                console.log(myJson)
            }
        }

        function post() {
            const userAction = async() => {
                const response = await fetch('http://scristovao.azurewebsites.net/api/Login', {
                    method: 'POST',
                    body: {
                        'UserID': '<>',
                        'Password': '<>'
                    }, // string or object
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                const myJson = await response.json(); //extract JSON from the http response
                // do something with myJson
                console.log(myJson)
            }
        }
    </script>
</head>

<body marginheight="0" marginwidth="0">
    <IMG NAME="imgBanner" WIDTH="100%" HEIGHT="100%" BORDER="0" cellspacing="0" cellpadding="0">
</body>

</html>

标签: javascripthtmlapi

解决方案


您的 get() 和 post() 永远不会调用 API。

您的函数创建了一个从未调用过的新函数。

        function get() {
            const userAction = async() => {} // This creates a new async function, which is not invoked

你甚至可以自己测试!

        function get() {
            const userAction = async() => {
                console.log('do get')  // Never gets logged when calling get()
            }

所以让我们删除函数包装

async function get() {
  console.log('do get')  // Now get logged when calling get()
  const response = await fetch('http://scristovao.azurewebsites.net/api/LastPositions', {
    method: 'GET',
    headers: {
      'Authorization': 'Bearer <BEARER_TOKEN>',
      'Access-Control-Allow-Origin': '*'
    }
  });
  const myJson = await response.json(); //extract JSON from the http response
  // do something with myJson
  console.log(myJson)
  return myJson
}

你的帖子功能是一样的。去掉包装后:

async function post() {
    const response = await fetch('http://scristovao.azurewebsites.net/api/Login', {
      method: 'POST',
      body: {
        'UserID': '<username>',
        'Password': '<password>'
      }, // string or object
      headers: {
        'Content-Type': 'application/json'
      }
    });
    const myJson = await response.json(); //extract JSON from the http response
    // do something with myJson
    console.log(myJson)
  return myJson
}

推荐阅读