ajax - 如何修复已被 api 阻止的 java 脚本中的 cors 策略
问题描述
我正在使用 html 创建登录页面,我的登录 api 是 REST API (django)。运行时显示 cors 策略已被阻止。如何修复此错误?我必须更改一些 java 脚本或 API。
var username=document.getElementById('UserName').value;
var password=document.getElementById('Password').value;
var http = new XMLHttpRequest();
var url = 'https://creativedsapi.kdns.in/hrapi/login';
var params={
"username":username,
"password":password
};
http.open('POST', url, true);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.setRequestHeader("Content-Type", "application/json");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState === 4 && http.status === 200) {
alert(http.responseText);
}
}
http.send(JSON.stringify(params));
解决方案
推荐阅读
- flutter - Flutter 是否支持 Android TV 和 Apple TV?
- javascript - 为什么我的 JS for-if 循环会得到错误的结果?
- c++ - std::如何与 std::placeholders 绑定到类成员函数
- python - Winmm.dll 不返回文件长度
- python - 如何将熊猫时间序列拟合到 24 小时图?
- css - 取决于屏幕大小的 CSS 元素
- python - get_text() 问题管理文本内的标签
- python - python中1和0的所有可能的3位组合
- flutter - 当我使用映射函数时,类型“(动态)=>动态”不是“测试”类型“(动态)=>布尔”的子类型
- amazon-web-services - 为什么软件 VPN 不利用现有的 Direct Connect 连接?