In my last article, we got an overview of Azure Functions. In this article, we are going to see how to consume an Azure function from the JavaScript code by handling the CORS.
What is CORS?
Cross-Origin Resource Sharing (CORS) is a mechanism to secure the cross-origin request and data transfer between the browser and the server. By default, Azure Functions do not allow all the cross-origin requests, which means if you call the Azure Function API from another domain, it will block the request. To allow other domains to access the function, we need to register those domains in allowed origins entry in the Azure Function. This can be done through Azure Portal.
Handling CORS in Azure Functions
HttpTrigger1 is the Azure Function which we created in my last article. Let’s check the response of the API using POSTMAN tool.

From the above image, you can notice we are passing a JSON payload to the function and it’s responded with some message for the request. Let’s access this function through JavaScript code.
Create an HTML page and write the below code.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title></title>
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js"></script>
- </head>
- <body>
- <h3> Azure function</h3>
- <label>Name: </label> <input type="text" id="txtName" />
- <button id="btnName">Click Me</button>
- <label id="message" style="color:green"></label>
- <script>
- $(document).ready(function () {
- $("#btnName").on("click", function () {
- $.ajax({
- type: "POST",
- url: "https://myfunctionappdemos.azurewebsites.net/api/HttpTrigger1?code=oRJcOc6j37H9vJKcdZzBRX95wKRsRIhdQaiPiKnNvVvUdgT9i0TYgw==",
- data: JSON.stringify({
- "name": $("#txtName").val(),
- }),
- success: function (response) {
- $("#message").text(response);
- },
- })
- });
- })
- </script>
- </body>
- </html>





Ninad JoshiPosted Oct 13, 2022, 8:34 AM
Hi I added the origin url still im getting the same error. any idea?
Shraddha AgrawalPosted Jul 20, 2020, 4:24 AM
If someone tries to access function from postman?
Yousef ShanawanyPosted Apr 23, 2019, 4:00 PM
Awesome thanks!
Madan ShekarPosted Jan 21, 2019, 3:37 AM
Nice article