从节点服务器向Flask API发送发布请求时遇到CORS问题

时间:2018-09-04 17:26:52

标签: javascript post cors

所以我已经设置了Flask API,并在其上启用了CORS

app = Flask(__name__)
CORS(app)

这是我的要求:

<input class="" type="file" name="Picture" onchange="sendPost()">
<script type="text/javascript">

function sendPost()
{
var data = document.getElementById("accidentPics");
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://cardamage-api.herokuapp.com/classify",true);
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Access-Control-Allow-Origin", "https://cardamage-api.herokuapp.com/classify")
xhr.withCredentials = true; 


xhr.send(data);

var body = XMLHttpRequest.response;

}

但是我一直遇到这个问题

Failed to load https://cardamage-api.herokuapp.com/classify: Response to 
preflight request doesn't pass access control check: The value of the 
'Access-Control-Allow-Credentials' header in the response is '' which must 
be 'true' when the request's credentials mode is 'include'. Origin 'null' is 
therefore not allowed access. The credentials mode of requests initiated by 
the XMLHttpRequest is controlled by the withCredentials attribute.

我还在chrome上使用CORS扩展名,所以我对为什么会发生这种情况感到有些困惑。我是Web开发的新手,所以任何建议都是不错的。

0 个答案:

没有答案