简体   繁体   English

加载表单数据并将其作为 json 传递给 api

[英]loading form data and passing it as a json to api

I am trying to use azure personalize and make food predictions.我正在尝试使用 azure 个性化并进行食物预测。 I have attached the code I am using without the API key.我附上了没有 API 密钥的代码。

I want to use the forms and get the contextFeatures and actions to pass into API rather than writing statically and rank them using azure-personalize API.我想使用 forms 并将contextFeaturesactions传递到 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>

You can check for HTTP triggers webhook and bindings to use request and response objects您可以检查 HTTP 触发 webhook 和绑定以使用请求和响应对象

Refer to this doc from Microsoft.请参阅 Microsoft 的此文档

As we can get the details as properties (body, headers, method).因为我们可以将详细信息作为属性(正文、标题、方法)。

In the below way we can access the request and response.通过以下方式,我们可以访问请求和响应。

// You can access your HTTP request off the context ...
if(context.req.body.emoji === ':pizza:') context.log('Yay!');
// and also set your HTTP response
context.res = { status: 202, body: 'You successfully ordered more coffee!' };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM