Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 Failed to load http://example.com/sendemail: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dev.example.com' is therefore not allowed access.
- 2. XHR finished loading: OPTIONS "http://example.com/sendemail".
- $.ajax({
- type: "POST",
- url: "http://example.com/sendemail",
- data: JSON.stringify(data),
- dataType : "json",
- contentType: "application/json; charset=utf-8",
- success: function(data){
- console.log('success');
- },
- failure: function(errMsg) {
- console.log('failure');
- }
- });
- [HttpOptions]
- [AllowCrossSiteJson]
- [HttpPost]
- public ActionResult SendEmail(string email, string name) { //code }
- Access-Control-Allow-Origin: *
- Access-Control-Allow-Methods: POST
- Access-Control-Allow-Headers: Content-Type
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
- HttpContext.Current.Response.Cache.SetNoStore();
- filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
- string rqstMethod = HttpContext.Current.Request.Headers["Access-Control-Request-Method"];
- if (rqstMethod == "OPTIONS" || rqstMethod == "POST")
- {
- filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
- filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With, Accept, Access-Control-Allow-Origin, Content-Type");
- }
- base.OnActionExecuting(filterContext);
- }
- Allow:OPTIONS, TRACE, GET, HEAD, POST
- Connection:keep-alive
- Content-Length:0
- Date:Wed, 24 Jan 2018 05:04:32 GMT
- Public:OPTIONS, TRACE, GET, HEAD, POST
- Server:Microsoft-IIS/10.0
- X-Powered-By:ASP.NET
- Microsoft.AspNet.Cors
- Microsoft.Owin.Cors
- public partial class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- ConfigureAuth(app);
- app.UseCors(CorsOptions.AllowAll);
- }
- }
Add Comment
Please, Sign In to add comment