javascript - Javascript - 获取到返回“来自原点‘null’的 API 已被 CORS 策略阻止”
问题描述
我正在使用以下 javascript 来执行 API 调用并根据响应执行不同的操作:
const passwordApiUrl = 'https://www.test.net/youin/api/password/'
// When RESET button clicked
function onReset() {
const urlParams = new URLSearchParams(window.location.search);
const resetCode = urlParams.get('resetcode');
const password = $('#txtPassword').val();
const apiPath = passwordApiUrl + resetCode;
alert(apiPath);
sendPostRequest(apiPath, password)
.then(response => {
console.log(response.status);
if (response.status !== 200) {
// Error
alert('error');
} else {
// Fine
alert('pass');
}
})
}
async function sendPostRequest(url, data) {
return await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}
This is the full error message in the console - ccess to fetch at ' https://www.test.net/youin/api/password/b0dBNnFlVVQ0TWorZklTbnBreGdkYnZLMlF2MTJ0R3g2VEd3RlZRa2Jia3lLeWNRVkpGTW5oVkw1K3hqR01xa3dUZWtrWFVzQmhLVEFSU280SFlaS2F4OTdLalg2Z1p1dFg5YU9YN3huTzA9 ' from origin 'null' has been blocked by CORS policy: Request header field content-预检响应中的 Access-Control-Allow-Headers 不允许类型。
关于如何修复的任何建议?
解决方案
推荐阅读
- bash - 无法使用 bash 脚本创建别名
- c++ - 我应该使用什么条件使我的代码与局部变量一起工作?
- gwt - 取消 GWT RPC 请求:设计
- ios - UICollectionView 的索引路径日期
- python - Popen 不同版本的进程间通信
- angular - 向 Angular 组件添加服务时遇到问题
- c# - 基类中缺少接口方法... COM?
- r - Sweave v. Knitr v. Rmarkdown:代码块头
- python-2.7 - Scapy --TypeError: unbound 方法 copy() 必须以 UDP 实例作为第一个参数调用
- java - 强制执行打印顺序,但线程在一次迭代后相互等待