跨源POST请求不适用于Outlook API

时间:2017-12-18 03:36:56

标签: javascript jquery ajax http-post outlook-api

我正在使用this part of the Outlook API。它说你应该能够做一个帖子请求,但是当我尝试时我得到以下错误:

Failed to load https://login.microsoftonline.com/common/oauth2/v2.0/token: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3003' is therefore not allowed access. The response had HTTP status code 400.

我该如何解决这个问题?我显然无法访问Outlook的服务器,但他们肯定会让我做一个帖子请求,考虑这就是文档中的说法!

如果有帮助的话,这是我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  let url = new URL(window.location);
  let code = url.searchParams.get('code');
  let redirect = 'http%3A%2F%2Flocalhost%3A3003%2Fauth-process-token.html';
  let clientId = '<MY ID>';
  let clientSecret = '<MY KEY>';

  var req_string = 'grant_type=authorization_code&code=' + code + '&redirect_uri=' + redirect + '&client_id=' + clientId + '&client_secret=' + clientSecret;

  $.ajax({
    type: 'POST',
    url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
    crossDomain: true,
    data: req_string,
    dataType: 'application/x-www-form-urlencoded',
    success: function (responseData, textStatus, jqXHR) {
      var value = responseData.someKey;
    },
    error: function (responseData, textStatus, errorThrown) {
      console.log('POST failed.', errorThrown);
    }
  });
</script>

编辑:我修正了“错误请求”错误,但它仍然给了我另一个错误。

1 个答案:

答案 0 :(得分:0)

一种解决方法是使用cors.io

http://cors.io/?http://your_link

所以它会是

http://cors.io/?https://login.microsoftonline.com/common/oauth2/v2.0/token