【发布时间】:2021-12-22 10:06:13
【问题描述】:
我正在尝试使用 azure 个性化并进行食物预测。我附上了我正在使用的没有 API 密钥的代码。
我想使用表单并将contextFeatures 和actions 传递给API,而不是静态编写并使用azure-personalize API 对它们进行排名。
var myHeaders = new Headers();
myHeaders.append(
"Ocp-Apim-Subscription-Key",
"xxxxxxxxxxxxxxxxxxxxxxxxx"
);
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
contextFeatures: [
{
timeOfDay: "Morning",
},
],
actions: [
{
id: "NewsArticle",
features: [
{
type: "News",
},
],
},
{
id: "SportsArticle",
features: [
{
type: "Sports",
},
],
},
{
id: "EntertainmentArticle",
features: [
{
type: "Entertainment",
},
],
},
],
excludedActions: ["SportsArticle"],
eventId: "75269AD0-BFEE-4598-8196-C57383D38E10",
deferActivation: false,
});
var requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
fetch(
"https://ibs-recommendations.cognitiveservices.azure.com/personalizer/v1.0/rank",
requestOptions
)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log("error", error));
<html>
<body>
<script src="index.js"></script>
<form>
<label for="fname">Food:</label>
<br />
<input type="text" id="fname" name="fname" />
<br />
<label for="lname">Ingriends one:</label>
<br />
<input type="text" id="lname" name="lname" />
<br />
<label for="lnametwo">Ingriend two:</label>
<br />
<input type="text" id="lnametwo" name="lnametwo" />
<br />
<label for="quantity">Amount of food</label>
<br />
<input type="number" id="quantity" name="quantity" />
<br />
<label for="time">Pick the time:</label>
<select id="time" name="time">
<option value="breakfast">BreakFast</option>
<option value="lunch">Lunch</option>
<option value="dinner">Dinner</option>
</select>
<br />
<input type="submit" />
</form>
</body>
</html>
【问题讨论】:
-
您的问题是什么?代码有问题吗?你调试了吗?
-
代码没有错。在 javascript 文件中,我在
JSON.stringify中手动将原始数据作为contextFeatures和actions提供,但我希望它取自 @Andy 的形式
标签: javascript html json azure forms