$.ajax No ‘Access-Control-Allow-Origin’ header is present on the requested resource with WebAPI

I want to get data from a WebAPI with jquery

        $("#btnSend").click(function () {
                type: 'GET',
                url: '/Report/SendEmail?quote=18',
                crossDomain: true,
                success: function (msg) {
                    if (msg == 'True') {
                        alert('Email sent to the client');
                error: function (request, status, error) {

and it produce

‘No Access-Control-Allow-Origin’ header is present on the requested resource error.

The solution is to add in the result of the WebAPI the following code:

            Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST");
            Response.Headers.Add("Access-Control-Allow-Headers", "accept, authority");
            Response.Headers.Add("Access-Control-Allow-Credentials", "true");

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.