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

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

问题描述

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

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# ).

我做了什么:

我获取了文本框值,并通过AJAX调用了Function,但它完全没有命中.它会发回400错误代码.

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

我只想使用Javascript来实现该功能.因为我只有一个简单的HTML页面,而JS是我可以与之交流的唯一手段.

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.

这是功能URL

https:///smtpgmailnet. = 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

这是我在firefox中发现的

This is what i found in firefox

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

Please let me know if you need any more information

推荐答案

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

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:

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

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天全站免登陆