Guest User

Untitled

a guest
Jan 23rd, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. 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. 2. XHR finished loading: OPTIONS "http://example.com/sendemail".
  3.  
  4. $.ajax({
  5. type: "POST",
  6. url: "http://example.com/sendemail",
  7. data: JSON.stringify(data),
  8. dataType : "json",
  9. contentType: "application/json; charset=utf-8",
  10. success: function(data){
  11. console.log('success');
  12. },
  13. failure: function(errMsg) {
  14. console.log('failure');
  15. }
  16. });
  17.  
  18. [HttpOptions]
  19. [AllowCrossSiteJson]
  20. [HttpPost]
  21. public ActionResult SendEmail(string email, string name) { //code }
  22.  
  23. Access-Control-Allow-Origin: *
  24. Access-Control-Allow-Methods: POST
  25. Access-Control-Allow-Headers: Content-Type
  26.  
  27. public override void OnActionExecuting(ActionExecutingContext filterContext)
  28. {
  29. HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
  30. HttpContext.Current.Response.Cache.SetNoStore();
  31.  
  32. filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
  33.  
  34. string rqstMethod = HttpContext.Current.Request.Headers["Access-Control-Request-Method"];
  35. if (rqstMethod == "OPTIONS" || rqstMethod == "POST")
  36. {
  37. filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
  38. filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With, Accept, Access-Control-Allow-Origin, Content-Type");
  39. }
  40. base.OnActionExecuting(filterContext);
  41. }
  42.  
  43. Allow:OPTIONS, TRACE, GET, HEAD, POST
  44. Connection:keep-alive
  45. Content-Length:0
  46. Date:Wed, 24 Jan 2018 05:04:32 GMT
  47. Public:OPTIONS, TRACE, GET, HEAD, POST
  48. Server:Microsoft-IIS/10.0
  49. X-Powered-By:ASP.NET
  50.  
  51. Microsoft.AspNet.Cors
  52. Microsoft.Owin.Cors
  53.  
  54. public partial class Startup
  55. {
  56. public void Configuration(IAppBuilder app)
  57. {
  58. ConfigureAuth(app);
  59.  
  60. app.UseCors(CorsOptions.AllowAll);
  61. }
  62. }
Add Comment
Please, Sign In to add comment