Getting express server to accept CORS request
I use cors and implement it so, it’s very simple var cors=require(‘cors’); app.use(cors({origin:true,credentials: true}));
I use cors and implement it so, it’s very simple var cors=require(‘cors’); app.use(cors({origin:true,credentials: true}));
The server at x3.chatforyoursite.com needs to output the following header: Access-Control-Allow-Origin: http://www.example.com Where http://www.example.com is your website address. You should check your settings on chatforyoursite.com to see if you can enable this – if not their technical support would probably be the best way to resolve this. However to answer your question, you need the … Read more
I’ve written an article with a complete CORS setup. I found several issues that can result in this problem: The Access-Control-Allow-Origin cannot be a wildcard if credentials are being used. It’s easiest just to copy the Origin header of the request to this field. It’s entirely unclear why the standard would disallow a wildcard. Firefox … Read more
I have found a solution. for some reason the default transportation method is not always allowed by all servers. So i specified a neutral transportation method at the client side, like this: var socket = io(‘https://yourDomain:3000’, { transports : [‘websocket’] });
I have found the source of confusion. It seems, WebAPI by default is using this exception handler: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/ExceptionHandling/DefaultExceptionHandler.cs and it has major differences from the suggested exception handling in this article: http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling see chapter “Appendix: Base Class Details”, where the code of default exception base class is given. In the example it checks for IsOutermostCatchBlock … Read more
WebSockets will not make AJAX entirely obsolete and WebSockets can do cross-domain. AJAX AJAX mechanisms can be used with plain web servers. At its most basic level, AJAX is just a way for a web page to make an HTTP request. WebSockets is a much lower level protocol and requires a WebSockets server (either built … Read more
FWIW, this is my CORS Middleware that works for my needs. func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set(“Access-Control-Allow-Origin”, “*”) c.Writer.Header().Set(“Access-Control-Allow-Credentials”, “true”) c.Writer.Header().Set(“Access-Control-Allow-Headers”, “Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With”) c.Writer.Header().Set(“Access-Control-Allow-Methods”, “POST, OPTIONS, GET, PUT”) if c.Request.Method == “OPTIONS” { c.AbortWithStatus(204) return } c.Next() } }
This has now been implemented in version 2.0.0. In your ConfigureServices use the following: options.AddPolicy(“MyCorsPolicy”, builder => builder .SetIsOriginAllowedToAllowWildcardSubdomains() .WithOrigins(“https://*.mydomain.com”) .AllowAnyMethod() .AllowCredentials() .AllowAnyHeader() .Build() ); Also, don’t forget to call UseCors in your Configure call too: app.UseCors(“MyCorsPolicy”);
if you are building your rest api in nodejs. Follow the folowing simple steps Stop the Node.js server. npm install cors –save Add following lines to your server.js or index.js var cors = require(‘cors’) app.use(cors()) // Use this after the variable declaration Now try to make your api call on the client side and it … Read more