javascript - Cross-Site AJAX using CORS on IIS (ASP.net) fails when setRequestHeader is used -


ok, trying enable cors on asp.net server can test javascript against web api using jsfiddle. web forms page calls enablecors function (below) on pageinit event.

public sub enablecors(optional origin string = "", optional methods string = "", optional headers string = "")     response.headers.add("access-control-allow-origin", vbnvl(origin, "*"))     response.headers.add("access-control-allow-methods", vbnvl(methods, "*"))     response.headers.add("access-control-allow-headers", vbnvl(headers, "*"))     response.headers.add("access-control-expose-headers", "access-control-allow-origin") end sub 

the vbnvl function replaces empty string second parameter value. so, values first 3 headers end being "*" if no parameters passed sub.

using jsfiddle, works great until append header values request object, fails saying "cors header 'access-control-allow-origin' missing". please take @ fiddle, , toggle setheaders variable false on line 1 see succeed. line 31 variable being used. super simple example.

can please me figure out why fails when use setrequestheader method on xmlhttprequest object? thanks!

have tried adding enablecors attribute top of webapi contronller, i.e.

[enablecors(origins: "http://youroriginaddress.com", headers: "*", methods: "*")] 

the following tutorial should of also:

http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

let me know how goes. ;)


Comments