Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read from server. It may not have the appropriate access-control-origin settings. ? #146

Closed
interpegasus opened this issue Feb 12, 2013 · 29 comments

Comments

@interpegasus
Copy link

Hi,

The server returns the following message:
"Can't read from server. It may not have the appropriate access-control-origin settings."

The API URL is: http://swagger-espn.mobovivo.com/api-docs.json

Could you please help to fix the issue?

Thanks in advance
Screen Shot 2013-02-11 at 10 54 09 PM

SPEC FILE

{
"basePath": "http://espn-server.herokuapp.com/api",
"swaggerVersion": "0.1a",
"apiVersion": "1.0",
"apis": [
{
    "path": "/info.json",
    "format": "json",
    "description": "Get API info and current match",
    "operations": [
        {
            "httpMethod": "GET",
            "summary": "Get API info and current match",
            "nickname": "info",
            "tags": [
                "production"
            ],
            "deprecated": false
        }
    ]
},
{
    "path": "/info/{id}.json",
    "format": "json",
    "description": "Get API info and current event",
    "operations": [
        {
            "httpMethod": "GET",
            "summary": "Show Event Info.",
            "nickname": "showByIdOne",
            "tags": [
                "production"
            ],
            "deprecated": false,
            "parameters": [
                {
                    "name": "id",
                    "description": "id = 1",
                    "dataType": "integer",
                    "allowMultiple": false,
                    "required": true,
                    "paramType": "path"
                }
            ],
            "errorResponses": [
                {
                    "reason": "Not found",
                    "code": 404
                },
                {
                    "reason": "Invalid ID",
                    "code": 400
                }
            ]
        }
    ]
}
]
}
@fehguy
Copy link
Contributor

fehguy commented Feb 12, 2013

Your server is not allowing CORS requests:

curl -i "http://swagger-espn.mobovivo.com/api-docs.json"
HTTP/1.1 200 OK
Date: Tue, 12 Feb 2013 05:14:27 GMT
Server: Apache/2.2.20 (Ubuntu)
Last-Modified: Tue, 12 Feb 2013 04:02:39 GMT
ETag: "738ec-154-4d57f18baad57"
Accept-Ranges: bytes
Content-Length: 340
Content-Type: application/json

In order to run the swagger-ui from a host other than mobovivo.com, you'll have to return the proper header values for Access-Control-Allow-Methods and Access-Control-Allow-Origin, like such:

curl -i "http://api.wordnik.com/v4/resources.json"
HTTP/1.1 200 OK
Access-Control-Allow-Headers: Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, *
Access-Control-Allow-Methods: POST, GET, OPTIONS , PUT
Access-Control-Allow-Origin: *
Access-Control-Request-Headers: Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport,  *
Content-Type: application/json
Date: Tue, 12 Feb 2013 05:14:27 GMT
Content-Length: 319
Connection: keep-alive

@interpegasus
Copy link
Author

Thanks, I enabled CORS and it works! 👍

@fehguy
Copy link
Contributor

fehguy commented Feb 12, 2013

great news

@gaffa
Copy link

gaffa commented May 8, 2013

Could you please tell me how? I have a pretty standard webapp running on tomcat (6/7). I always get that message and I do not really understand what I have to do.

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 08 May 2013 16:20:52 GMT

{"apiVersion":"0.1-ALPHA","swaggerVersion":"1.0","basePath":"http://localhost:9999/","apis":[{"path":"/api-docs/rest/api/order"}]}

@marcellodesales
Copy link

Just got mine working... Awesome!

@toshim45
Copy link

pls re-open this issue, I got mine working from localhost, but if I access it from another ip address it will return can't read from server....
swagger-ui-can-not-access
localhost version
swagger-ui-localhost
And this is my response header from localhost :

Access-Control-Allow-Headers:Content-Type, api_key, Authorization, x-requested-with, Total-Count, Total-Pages, Error-Message
Access-Control-Allow-Methods:POST, GET, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1800

Am I missing something ?

Thanks in advance

@webron
Copy link
Contributor

webron commented Aug 19, 2014

@toshim45 - what's the output you're getting when running:

curl -i "http://localhost:8080/pena-im/api-docs"

@toshim45
Copy link

hi @webron

$ curl -i "http://localhost:8080/pena-im/api-docs"
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, DELETE, PUT, OPTIONS
Access-Control-Allow-Headers: Content-Type, api_key, Authorization, x-requested-with, Total-Count, Total-Pages, Error-Message
Access-Control-Max-Age: 1800
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 19 Aug 2014 19:47:47 GMT
{"apiVersion":"1.0","swaggerVersion":"1.2","apis":[{"path":"/default/compose-controller","description":"Compose Controller"},{"path":"/default/home-controller","description":"Home Controller"},{"path":"/default/inbox-controller","description":"Inbox Controller"}],"info":{"title":"default Title","description":"Api Description","termsOfServiceUrl":"Api terms of service","contact":"Contact Email","license":"Licence Type","licenseUrl":"License URL"}}

@toshim45
Copy link

sorry my mistake, my configuration is wrong in index.html :) it should be ip address, not localhost.
thanks @webron

@gandarez
Copy link

gandarez commented Sep 5, 2014

You rock man!

@jhambj
Copy link

jhambj commented Dec 6, 2014

Hi Guyz, I just started using swagger, can you help me understand how do I use it to document my REST API's? Thanks in advance!!

@jhambj
Copy link

jhambj commented Dec 6, 2014

I got it downloaded from github , but now how to initiate to make it work for my own API's instead of petstore already existing one

@webron
Copy link
Contributor

webron commented Dec 6, 2014

@jhambj - Please send a post to https://groups.google.com/forum/#!forum/swagger-swaggersocket with the details of your application, and I'll help you from there.

@jhambj
Copy link

jhambj commented Dec 6, 2014

just doing that but even I am not sure what all details are needed though leaving a post there.Thank you so much.

@SuperMarioX
Copy link

Hi, I could solve this issue on Tomcat server, but it remains a problem on websphere application server profile as a server. Do you have any way to fix this?

Many thx :-)

@webron
Copy link
Contributor

webron commented May 22, 2015

Not sure why websphere would be any different. Enabling CORS is normally done at the application level, though tomcat has its own filter you may have used. Just enable it on websphere and you should be done.

@senid231
Copy link

Why we need to accept CORS for fetching json file?
you don't need it for AJAX GET

@fehguy
Copy link
Contributor

fehguy commented Nov 28, 2015

You don't need CORS for a GET. Swagger-js (the underlying library for the UI) does not differentiate in the request process and therefore sends a preflight request.

@icwatk
Copy link

icwatk commented Dec 21, 2015

where do you put the bit of code in to allow CORS?

Thanks!

@webron
Copy link
Contributor

webron commented Dec 21, 2015

@icwatk - that depends on how you serve your swagger.json and API. no single answer to it.

@kkirsche
Copy link
Contributor

I would recommend in the error message including a link to a resource or two to help users who hit this issue more quickly solve the issue they are seeing.

@webron
Copy link
Contributor

webron commented Oct 13, 2016

@kkirsche - while I agree that when it comes to user experience it would be better, there's also this - https://github.com/swagger-api/swagger-ui#or-how-to-deal-with-cant-read-from-server-it-may-not-have-the-appropriate-access-control-origin-settings. It's right there in the README of this project.

@kkirsche
Copy link
Contributor

Which doesn't help to individuals running this for a company. The readme only is helpful to the deployer

@shaaasiiingh
Copy link

Hi,

The server returns the following message: when trying to fetch swagger data in html format.
"Can't read from server. It may not have the appropriate access-control-origin settings."
project structure-swaggerdemo/man/resources/webapp/index.html

The API URL is: http://localhost:8080/config/config/swagger.json
Although, i'm able to fetch when given swagger.json or swagger.yaml

index.html-
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://localhost:8080/config/config/swagger.json";
}
Swaggerconfig.java
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/config");
beanConfig.setResourcePackage("yourpackage");
beanConfig.setTitle(" Swagger UI");
beanConfig.setDescription("swagger ui example.");
beanConfig.setScan(true);
beanConfig.setPrettyPrint(true);

web.xml

jersey
org.glassfish.jersey.servlet.ServletContainer

jersey.config.server.provider.packages

io.swagger.jaxrs.listing,
com.oracle.gbu.odi.controller


  <servlet>
      <servlet-name>SwaggerConfigurationServlet</servlet-name>
      <display-name>SwaggerConfigurationServlet</display-name>
      <servlet-class>package</servlet-class>
      <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>jersey</servlet-name>
      <url-pattern>/config/*</url-pattern>
  </servlet-mapping>

Could you please help to fix the issue?

Thanks in advance

@Geralt-x
Copy link

it's very easy.What you should do is change "localhost" to "127.0.0.1"!Then you will success.

@AssiaHalloul
Copy link

hey i'm working with swagger hub but when i click on try it out for an endpoint i got this error ,could you help me please

access-control-allow-credentials: true access-control-allow-headers: X-Requested-With,Content-Type,Accept,Origin access-control-allow-methods: * access-control-allow-origin: * cache-control: no-cache content-length: 0 content-type: application/octet-stream date: Wed,28 Apr 2021 17:25:24 GMT etag: W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk" expires: -1 server: Jetty(9.4.29.v20200521) status: 404 Not Found x-firefox-spdy: h2 x-powered-by: Express,Phusion Passenger(R) 6.0.8

@quynguyenhcm
Copy link

Hi, I can access to swagger doc from localhost. However, when host the app within Docker container and do a port forward from localhost to the container, I got the same message "Can't read from server. It may not have the appropriate access-control-origin settings."

Can someone suggest solution please?

Thanks
Quy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests