javascript - CORS error for firefox and chrome -


i have problem regarding cors error in code,the .js file run gives error chrome , firefox not on ie. have been suggested add response headers confused add it.please help

xhr.open(method, url, true); 

i tried adding xhr.setrequestheader("access-control-allow-origin:*"); it,will work?

do not add xhr.setrequestheader("access-control-allow-origin:*") @ client side code.

to allow cross domain request, server needs add access-control-allow-origin header response.

the value of access-control-allow-origin should domain name of request allowed or * if origin allowed.

access-control-allow-origin: http://domain1.com or

access-control-allow-origin: *

if calling web api method refer below tested code , working rest api.

client side code ajax call:

function callajax() {        var xhr = new xmlhttprequest();  xhr.open("get", "http://localhost:54118/api/values", true);  xhr.onload = function () {      console.log(xhr.responsetext);      alert("sucuess");  };  xhr.send();  }

server side changes :

in web api project have add following configuration in web.config file. under system.webserver section add following

 <httpprotocol>        <customheaders>          <add name="access-control-allow-origin" value="*"/>        </customheaders>      </httpprotocol>

this allow domain access resource.

hope helpful :)


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -