Forum Discussion

1cookie's avatar
1cookie
Occasional Contributor
4 years ago

Change domain for 'try it out' feature

Hi,

So currently I'm using the standalone version of swaggerUIBundle:

window.onload = function () {
   // Begin Swagger UI call region
   const ui = SwaggerUIBundle({})
}

 On the front-end. When I enter the "try it out" feature and press the execute button; the request URL and the cURL string are both using the same domain which my page is on. My API sits on another domain however and so the response returns a page not found (as you would expect). Is there a config setting allowing me to serve my APIs from another domain?

 

Thanks

 

Thanks

  • You need to enable CORS on your API server, localhost:8007. The way you do this depends on the server and framework used, check out https://enable-cors.org/server.html

     

    Alternatively, hosting Swagger UI on the same server:port as the API server will avoid CORS altogether.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    You need to specify the target server in your API definition, for example:

     

    openapi: 3.0.0
    servers:
      - url: 'https://example.com/api'

     

    or

     

    swagger: '2.0'
    
    host: example.com
    basePath: /api
    schemes:
      - https

     

     

    • 1cookie's avatar
      1cookie
      Occasional Contributor

      hi,

      Thanks for that. Using your first option - the URLs are now pointing at my API on the correct domain. I still have an issue where the server fails to load the response:-

       

       

       

      I've checked the above in my local browser and the payload returns successfully.

       

      Thanks

       

       

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        What's the error message in the Console tab of the browser developer tools?