gpt4 book ai didi

Azure认知搜索通过浏览器上传文档cors问题

转载 作者:行者123 更新时间:2023-12-03 06:58:40 26 4
gpt4 key购买 nike

我想使用认知搜索来存储浏览器游戏的用户名和级别。应该有一个选项可以在网络前端创建新用户名。这个想法是,用户输入用户名并通过按钮提交。那么这些用户名应该保存在认知搜索文档中。

MyiIssue:我在浏览器控制台中收到 cors 错误:

Access to XMLHttpRequest at 'https://XXXXXXX.search.windows.net/indexes/users/docs/index' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

当我将其部署到 azure 静态 Web 应用程序时,也会出现同样的问题。

azure 上的 CORS 设置允许所有来源。

关于Azure Search and CORS issue with PUT ,无法通过这种方式修改数据。我是编程新手,如何以其他方式完成(无服务器函数?)?

我的代码:

OnSubmitCreate(){
console.log('Create started')
let data = JSON.stringify({
"value": [
{
"@search.action": "mergeOrUpload",
"id": this.createName,
"username": this.createName,
"level": 1
}
]
});

let config = {
method: 'post',
url: 'https://XXXXXXXXXXX.search.windows.net/indexes/users/docs/index?api-version=2020-06-30',
headers: {
'api-key': 'XXXXXXXXXXXXXXXXXxx',
'Content-Type': 'application/json'
},
data : data
};

提前致谢!

最佳答案

我已经通过使用 javascript 创建 azure 函数解决了这个问题。当我调用该 URL 时,就会完成对认知搜索的写入。

所以从技术上讲,我正在执行 http 休息调用(推送)。

index.js

var https = require('https');

module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');

var username = context.bindingData.username;
var level = context.bindingData.level;

let data = JSON.stringify({
"value": [
{
"@search.action": "mergeOrUpload",
"id": username,
"username": username,
"level": level
}
]
})

var options = {
host: 'XXXXXXXXXXX.search.windows.net',
path: '/indexes/users/docs/index?api-version=2020-06-30',
port: 443,
method: 'POST',
headers: {
'api-key': 'XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: data
};


var req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(data);
req.end();

context.res = {
// status: 200, /* Defaults to 200 */
body: req
};

}

函数.json

{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "data/{username:alpha}/{level:int?}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}

关于Azure认知搜索通过浏览器上传文档cors问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72666922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com