0

I'm making a request from client side to a web-API on different domain to extract data in JSON format. How do I enable Cross Origin Resource Sharing(CORS)? Client runs on https while my web-API runs on http.

This is the AJAX call that I'm making :

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://map.techriff.in/api/values",
        success: function (json) {
            console.log(json);
        },
        error: function (err) {
            console.log(err);
        }
    });
});
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Ron777
  • 11
  • 1
  • 2
  • https://www.google.ie/?gws_rd=cr,ssl&ei=pW9pVauLMpGR7Ab8voGoAw#q=ASP.NET+WebAPI+How+to+enable+CORS+%3F – George Vovos May 30 '15 at 08:07
  • 1
    CORS is already enabled. The API is capable of serving http request only. We want to enable it for https. Is there any way?? – Ron777 May 30 '15 at 08:16
  • Your question miss a fundamental information: how are you enabling CORS in the server side, and for which domains? According to your previos comment you're enabling it. How? If we don't seeit, why can't know why its failing. – JotaBe Jun 01 '15 at 08:38

4 Answers4

1

This site helped me when I had an issue with Chrome showing the following error: "No 'Access-Control-Allow-Origin' header is present on the requested resource"

Go down to the section titled "Enable CORS".

https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Note, I used the following attribute syntax as opposed to what was listed in the site above:

        [EnableCors("http://localhost:1616", "*", "*")]
jbizzle
  • 1,495
  • 2
  • 11
  • 11
0

You need to add the Access-Control-Allow-Origin: http://domain.com to your response header, where domain.com is replaced with the domain you want to allow (don't use * wildcards).

How you do this depends one your server stack. In ASP.NET:

Response.AppendHeader("Access-Control-Allow-Origin", "http://domain.com");

You then need to set $.support.cors = true in your jQuery to enable it on the client.

garryp
  • 5,508
  • 1
  • 29
  • 41
0

Add $.support.cors = true; somewhere before to make your $.ajax call.

Source: Is it safe to use $.support.cors = true; in jQuery?

Assuming you correctly set the Access-Control-Allow-Origin header on the server as well.

CORS jQuery AJAX request

Community
  • 1
  • 1
Vadorequest
  • 16,593
  • 24
  • 118
  • 215
0

First of all, this is a big issue. Everyone will say you have to enable CORS in the server. What if we are requesting an API?. What I did is.

Step 1: Make an ajax call to my own server. Step 2: Make https request from my server to the API. Step 3: Send the result to the ajax.

My AJAX call.

   $.ajax({
              type: "POST",
              url: "makepay",
              data:{
                          key:value
                     },
                     success: function(response) {
                         //place to handle the response

                          },
                          error: function() {
                             //place to handle the error
                          }
                }); 

My server page

const https = require('https');
app.post('/makepay',function(req, res){
  var options = {
  host: "Site address",
  path: "Path",
  method: "POST",
  headers: {
      'Content-Type': 'application/x-www-form-urlencoded',

  }
  var req = https.request(options, (resp) => {
  resp.on('data', (xmlresponse) => {
  res.send(xmlresponse);  
  }}
  req.write(parameters_to_the_API);
  req.end();

});

I hope you will get at least the idea.

shamnad
  • 29
  • 3
  • https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en download this plugin and make CORS. – shamnad Nov 12 '18 at 05:31