1
Answer

Success Function Return Error

Access-Control-Allow-Origin' header is present on the requested resource
 
My Code Is
 
function GetPaymentTypes() {
debugger;
var settings = {
"async": true,
"crossDomain": true,
"url": "URL",
"method": "POST",
"headers": {
"Content-Type":"application/json",
"Authorization":"Basic"
},
"processData": false,
}
$.ajax(settings).done(function (response) {
debugger;
console.log(response);
});
}
 
Answers (1)
0
Rupesh Kahane

Rupesh Kahane

100 19.1k 4.2m 6y
Refer & try below
 
1) https://www.html5rocks.com/en/tutorials/cors/#toc-types-of-cors-requests
 
2) Do you try to modified the web.config for the webservice to add the following line yet ?
 
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol>
 
3) I found the solution to my problem. Seems like I haven't been paying full attention of the error message properly :)
 
After I added the Access-Control-Allow-Origin header into my web.config file, it turns out I started getting some other error, which was content-type custom header was missing and I thought I was still getting the CORS error.
 
So, adding the <add name="Access-Control-Allow-Origin" value="*" /> definition into my web.config actually solved my cross-origin problem in the first place! And I also have tested to allow specific domains or requests from ports with the following web.config header entry and it also worked like charm - since I think it is the best option to use since I will not be publishing my services publicly.
 
<add name="Access-Control-Allow-Origin" value="http://localhost:17256" />
 
4) https://stackoverflow.com/questions/12296910/so-jsonp-or-cors
 
5) Or just add the proper header in your web.config from your web api site:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3. <system.webServer>  
  4. <httpProtocol>  
  5. <customHeaders>  
  6. <add name="Access-Control-Allow-Origin" value="*" />  
  7. <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />  
  8. </customHeaders>  
  9. </httpProtocol>  
  10. </system.webServer>  
  11. </configuration>  
6) Try
  1. $.ajax({  
  2. crossDomain: true,  
  3. contentType: "application/json; charset=utf-8",  
  4. type: "GET",  
  5. url: "https://mail.google.com/mail/feed/atom/",  
  6. dataType: "xml",  
  7. cache: false,  
  8. headers: {  
  9. "Authorization""Basic " + btoa(USERNAME + ":" + PASSWORD)  
  10. },  
  11. success: function (result) {  
  12. alert("XML File is loaded!");  
  13. alert(result);  
  14. }  
  15. });  
  16. // You need to specify your username and password.  
All the best