0

Am trying pull data from .net core web api using ajax call am getting the below error

Failed to load http://localhost:8085/api/menu: Response for preflight has invalid HTTP status code 404.

but am getting data sucessfully from postman.

am trying below code for ajax

$(document).ready(function(){
  $.ajax({ 
    url: 'http:/localhost:8085/api/menu', 
    type: 'GET', 
    dataType: 'json',
    data: '{ }',
    success: function (data) { 
        console.log(data); 
    },
    failure: function (data) {  
        alert(data.responseText);  
    }, //End of AJAX failure function  
    error: function (data) {  
        alert(data.responseText);  
    } 
  });
});

I have added below tag in web.config file

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
</httpProtocol>

Please suggest your thoughts. Thanx in advance.

1 Answer 1

4

you can define your policy in Startup file as a part of ConfigureServices method

for example:

{
  services.AddCors(c => {
      c.AddPolicy("policyName", p => {
          p.AllowAnyOrigin().AllowAnyMethod();
      });
  });
}

and then in Configure method

app.UseCors("policyName");

Sign up to request clarification or add additional context in comments.

6 Comments

Thanx for your reply i have tried above but still am getting different issue jquery.min.js:4 GET localhost:5000/localhost:8085/api/menu?{%20} 404 (Not Found)
you address includes localhost twice - I'm 99% sure there is a bug somewhere and it should probably be either localhost:5000 or localhost:8085
There is a bug i fixed and now working fine thank you again.
perfect, please mark this solution as an valid answer so it's visible for others who have the same problem that it works.
Is this to be configured in the caller webapp or the calee web api?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.