从 Javascript 调用 Azure 函数 [英] Call Azure Function from Javascript

查看:15
本文介绍了从 Javascript 调用 Azure 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 HTML 页面,它有一个文本框和一个提交按钮.我有一个 Azure 函数,当点击它时会接收文本框的内容,我们应该将其发送到 Azure 函数并发送邮件(代码是用 C# 中的 Azure 函数编写的).

我做了什么:

我获取了文本框的值并通过 AJAX 调用了函数,但它根本没有命中.它发回 400 错误代码.

我想要的只是使用 Javascript 来实现该功能.因为我只有一个普通的 HTML 页面,而 JS 是我可以交流的唯一方式.

这是函数网址

如果您需要更多信息,请告诉我

解决方案

这绝对是 CORS 相关问题.幸运的是,在门户中为功能应用程序配置 CORS 很容易.只需导航到您的 Function App,然后选择 Platform Features:

选择 API 下的 CORS 选项,这将带您进入一个屏幕,您可以在其中添加客户端应用程序的主机名:

I have a simple HTML page which has a Textbox and a Submit Button. I have a Azure Function which when hit takes in the content of the textbox, which we should send to the Azure Function and sends mail ( code is written in Azure Function in C# ).

What I Did :

I took the textbox value and called the Function via AJAX but it doesnt hit at all. It sends back 400 Error Code.

All i want is to hit the function using Javascript. Because i just have a plain HTML page and JS is the only means with which i can communicate.

This is the Function URL

https://smtpgmail.azurewebsites.net/api/HttpTriggerCSharp1?code=ihrbDCeo2DPdxPXDKiQWyNFYfaMFZhk9rkaYFaHO3LFsNEmrabj9Cw==

it expects a parameter 'name' and i checked it by pasting in the browser like this

https://smtpgmail.azurewebsites.net/api/HttpTriggerCSharp1?code=ihrbDCeo2DPdxPXDKiQWyNFYfaMFZhk9rkaYFaHO3LFsNEmrabj9Cw==&name=testName

This is what i found in firefox

Please let me know if you need any more information

解决方案

This is definitely a CORS related issue. Luckily, it's easy to configure CORS for Function Apps in the portal. Just navigate to your Function App and then select Platform Features:

Select the CORS option under API which will take you to a screen where you can add the hostname of your clientside application:

这篇关于从 Javascript 调用 Azure 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆