Need Help on Cors #13

Closed
sameersanatkpatro opened this Issue Sep 30, 2016 · 6 comments

Projects

None yet

3 participants

@sameersanatkpatro

Hi @kataras ,
I know this Question have been asked multiple times.But I am not able to understand how to implement the Iris.Any.
I have implemented my frontend in react and backend in iris.
When I am trying a PUT its throws CORS issue.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3333' is therefore not allowed access

I have configured CORS:

app := iris.New()
app.Use(cors.DefaultCors())

The API are as follows:
app.Post("/user/create",func(c *iris.Context){//db.insert...}
app.Put("/user/edit/:email",func(c *iris.Context){//db.update...}
.....
I have read on the previous answers that we need to user Iris.Any.
Can you please tell me how the Iris.Any will be implemented for the Api's above?Thanks in advance for your help.

@kataras kataras added the bug label Sep 30, 2016
@kataras
Member
kataras commented Sep 30, 2016

@sameersanatkpatro cors.DefaultCors() accepts only those two methods for cors: []string{"GET", "POST"}

you have to call it with options:

cors.New(cors.Options{AllowedMethods: []string("GET", "POST", "PUT"})

Please test that and write here the results

@kataras kataras self-assigned this Sep 30, 2016
@kataras kataras removed the bug label Sep 30, 2016
@sameersanatkpatro
sameersanatkpatro commented Sep 30, 2016 edited

Thanks @kataras for the reply. I have tried this option but I see it still fails.
"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3333' is therefore not allowed access. The response had HTTP status code 404."

Can you please help me on this?

@kataras kataras added a commit to kataras/iris that closed this issue Sep 30, 2016
@kataras kataras Fix iris-contrib/middleware#13 716b500
@kataras
Member
kataras commented Sep 30, 2016

Yes, after ~4 hours I fixed it, upgrade your iris and the middleware/cors test and tell me the results, ( I tested on dedicated server and it worked)

@kataras kataras referenced this issue in kataras/iris Sep 30, 2016
Closed

Need Help on Cors #449

@sameersanatkpatro

Thanks very much @kataras .It worked.

@kataras
Member
kataras commented Sep 30, 2016

You're welcome, the price is one star to the iris repository, this will help you to be up-to-date for any new changes :)

@sy264115809

Hi, @kataras

I encounter the same question mentioned at kataras/iris#486, but I can't comment that issue so I create a new one here.

Here's my code:

app := iris.New()
app.Use(cors.Default())
app.Post("/login", controller.Login) // login

private := app.Party("", middleware.Auth) // add my own auth middleware
resources := private.Party("resources") // create  a party represent a resources endpoint
resources.Get("", controller.List) // list all resource

When I send a request from front-end to try to get all resources(GET /resources), the Options send by chrome returns that:

XMLHttpRequest cannot load http://localhost:8898/api/v1/resources. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 404.

I read the source and find out that in here, iris only check the first tree which passes the check of methodEqual. Suppose that the tree is the POST tree, iris will not get a expected entry from this tree since the path /resources doesn't be added in this tree, and iris return 404 in the end of this method.

Seems before the request touching the CORS middleware, iris has already rejected it with 404

After I removing this line, the problem is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment