简体   繁体   English

如何将Azure Httptrigger连接到网站?

[英]How do I connect Azure Httptrigger to website?

I have limited experience with creating websites/front-end - just HTML/CSS - and work mostly on the backend - Node.js in Azure functions. 我在创建网站/前端(仅HTML / CSS)方面经验有限,并且主要在后端(Azure函数中的Node.js)上工作。

I have created an Azure function (httptrigger) that I would like a button on my website to hit. 我已经创建了一个Azure函数(httptrigger),我希望点击我网站上的一个按钮。

For example, like a search bar, I want a user to type something and click search. 例如,像搜索栏一样,我希望用户键入内容并单击搜索。 After they click search I want to hit the http trigger with the search contents in the query string. 在他们单击搜索之后,我想在查询字符串中单击带有搜索内容的http触发器。

After hitting my trigger, I would then want it to return some data to show on the webpage. 按下触发器后,我希望它返回一些数据以显示在网页上。

I am struggling with adding the search content to the query string (and perhaps securely hiding the httptrigger endpoint?) and how I would send data back to the html website. 我正在努力将搜索内容添加到查询字符串中(也许安全地隐藏了httptrigger端点?),以及如何将数据发送回html网站。

At the moment, I have built the httptrigger to work and return html data via context.res= {body: html} which works fine when I post the httptrigger url into my browser, but of course I want this to send data back and affect the current webpage with the search bar, by adding the results below it. 目前,我已经构建了httptrigger来工作,并通过context.res= {body: html}返回html数据,当我将httptrigger url发布到浏览器中时,它可以正常工作,但是我当然希望它将数据发送回去并影响通过将搜索结果添加到当前网页下方的搜索栏。

Any pointers or tips here would be greatly appreciated as I'm not too experienced sewing the front end together with this. 这里的任何指针或技巧都将不胜感激,因为我对将前端与此缝在一起不太有经验。

Thanks! 谢谢!

So my suggestion is to break this down a little bit and possibly create some new more specific questions based on that breakdown. 因此,我的建议是对此进行分解,并可能基于该分解来提出一些新的更具体的问题。 As it is this is pretty broad. 因为这是相当广泛的。

Realize that from your web site's (front end) perspective there isn't anything different about hitting and Azure Function Http Trigger then any other REST endpoint and the answer to how to wire it in on the front end is going to vary greatly based on any frameworks you may be using (sometimes those frameworks even want data returned in a specific way as well). 从网站(前端)的角度认识到,点击和Azure Function Http Trigger并没有什么不同,然后是其他任何REST终结点,并且如何将其连接到前端的答案将基于任何您可能正在使用的框架(有时这些框架甚至还希望以特定方式返回数据)。

With that knowledge in mind I would focus first on ensuring your Azure Function is properly configured and generating a JSON result you expect before trying to work with the UI. 考虑到这些知识,在尝试使用UI之前,我将首先关注确保正确配置Azure函数并生成期望的JSON结果。 I can give you some suggestions in that regard: 在这方面,我可以给您一些建议:

  • You mentioned "hiding" your endpoint. 您提到了“隐藏”端点。 You need to identify first how you want to secure it as you won't be able to "hide" it as long as it's on the internet. 您首先需要确定要如何保护它,因为只要它在Internet上就无法“隐藏”。 If it's an anonymous site you're going to have limited options. 如果它是一个匿名站点,那么您将拥有有限的选择。 If you are behind a login you have many more options but most best practices utilize some sort of token based approach (Oauth, Custom, etc). 如果您落后于登录名,则可以有更多选择,但是大多数最佳实践都采用了某种基于令牌的方法(Oauth,Custom等)。 Azure Functions do have the concept of Function Keys you can pass in but this really only applies to non browser usage of the APIs where they can be kept secret. Azure函数确实具有可以传递的函数键的概念,但这实际上仅适用于非浏览器对API的使用,可以将它们保密。 Any key you add into your front end would be visible to everyone and essentially useless (unless you just want to block folks randomly hitting your endpoints). 您添加到前端的任何密钥对于所有人都是可见的,并且基本上是无用的(除非您只是想阻止人们随机击中端点)。
  • Update the CORS settings in Azure Functions to ensure the domain(s) that are using your API are configured otherwise you'll get blocked by cross-domain restrictions in browsers. 更新Azure Functions中的CORS设置,以确保配置了使用您的API的域,否则您将被浏览器中的跨域限制所阻止。 More can be found at https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#cors 可以在https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-how-to-use-azure-function-app-settings#cors中找到更多信息
  • Create a function with some mock data in the format you want (you can circle back later and pull it from a real data source once you have it working). 用所需的格式创建一个包含一些模拟数据的函数(您可以稍后回圈,并在工作后将其从真实数据源中提取出来)。 Snippet example below using a couple hard coded states. 下面的代码段示例使用了几个硬编码状态。

 module.exports = function(context, req) { if (req.query.filter) { // Do something with the filter parameter when loading results context.res = { body: '[{"name": "New York","abbreviation": "NY"},{"name": "Ohio","abbreviation": "OH"}]', headers: { 'Content-Type': 'application/json' } }; } else { context.res = { status: 400, body: "Please pass a filter on the query string" }; } context.done(); }; 

Once you have a working function that returns back your data then move on to tackle your UI integration. 一旦具有可以返回数据的工作功能,然后继续进行UI集成。

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

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