You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure AI Search CORS问题:前端调用索引器接口遭拦截

调用Azure搜索索引器时遭遇CORS拦截错误

我基于azure-search-openai-demo搭建简易Demo,新增了一个带按钮的页面,点击按钮即可触发运行Azure门户中配置好的索引器。但即便使用了admin密钥,前端仍持续出现CORS拦截错误,具体错误信息如下:

从源 'http://127.0.0.1:50505' 访问 'https://searchServiceName.search.windows.net/indexers/indexerName/run?api-version=2020-06-30' 时被CORS策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。如果不透明响应满足你的需求,请将请求的模式设置为 'no-cors' 以在禁用CORS的情况下获取资源。

对应的React代码如下:

import React from "react";
import { DefaultButton } from "@fluentui/react";
import styles from "./Helloworld.module.css"; 

const HelloWorld: React.FC = () => {
    // 运行索引器按钮逻辑
    const runIndexer = async () => {
        const indexerName = "xxx"; // 索引器名称
        const searchServiceName = "xxx"; // AI搜索服务名称
        const apiKey = "xxx"; // API密钥

        const url = `https://${searchServiceName}.search.windows.net/indexers/${indexerName}/run?api-version=2020-06-30`;

        await fetch(url, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "api-key": apiKey
            }
        });

        console.log(`正在运行索引器 ${indexerName} .....`);
    };

    return (
        <div className={styles.container}>
            <h1>Hello World</h1>
            <DefaultButton onClick={runIndexer} text="运行索引器" className={styles.runIndexerButton} />
        </div>
    );
};

export default HelloWorld;

内容的提问来源于stack exchange,提问作者Angus Chang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 08:42:44