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
相关产品推荐
相关产品推荐

