首页 > 解决方案 > 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 不允许类型。

关于如何修复的任何建议?

标签: javascriptapifetch

解决方案


推荐阅读