如何使用jQuery进行跨域api调用

时间:2018-09-06 18:07:04

标签: javascript jquery ajax api

我正在尝试使用jquery $ .ajax进行API post调用,但是出现以下错误

Failed to load https://accounts.spotify.com/api/token: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://sontest.000webhostapp.com' is therefore not allowed access.

任何帮助将不胜感激。

$.ajax(
      'https://accounts.spotify.com/api/token', {
      type: 'POST',
      headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
                "Access-Control-Allow-Headers": "X-Requested-With,content-type",
                "Access-Control-Allow-Credentials": false
               },
               dataType: 'json',
               beforeSend: function (xhr) {
                  xhr.setRequestHeader('Authorization', 'Basic ' + spotifyDetails);
                  xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
               },
               data: "grant_type=client_credentials",
               complete: function (response) {
               },
               error: function (jqXHR, textStatus, errorThrown) {
                 console.log(errorThrown);
               }
});

0 个答案:

没有答案