swagger-api / swagger-js

Javascript library to connect to swagger-enabled APIs via browser or nodejs
http://swagger.io
Apache License 2.0
2.61k stars 759 forks source link

Using Swagger.js in the browser, preventing JSONP wrapper #53

Closed df-jsykes closed 10 years ago

df-jsykes commented 11 years ago

Currently when i make a cross domain call, it does so and returns the data in a callback. The server I'm hitting uses CORS, so the wrapper isn't needed. What can I do to let my server decide how to handle cross domain, not swagger-js, or am I missing something?

df-jsykes commented 10 years ago

This still leaves a default callback wrapper object.

Here is my index.html

<!DOCTYPE html>
<html>
<script src='lib/shred.bundle.js' type='text/javascript'></script>
<script src='lib/swagger.js' type='text/javascript'></script>
<script type="text/javascript">
    // initialize swagger, point to a resource listing

    window.onload=function () {
        window.df = new SwaggerApi({
            url:"https://next.cloud.dreamfactory.com/rest/api_docs",
            supportedSubmitMethods: ['get', 'post', 'put', 'patch', 'merge', 'delete']
        });

        window.authorizations.add("X-DreamFactory-Application-Name", new ApiKeyAuthorization("X-DreamFactory-Application-Name", "todoangular", "header"));
        window.authorizations.add('Content-Type', new ApiKeyAuthorization('Content-Type', 'application/json', 'header'));
        window.df.authorizations = window.authorizations;
        window.df.build();
    };
</script>
<head>
    <title></title>
</head>
<body>
<div id="mydata"></div>
</body>
</html>

in your console, you can try df.apis.system.getConfig(); to see the wrapper

I'm running this from the file system. Something wrong with my Swagger API itself?

df-jsykes commented 10 years ago

I do see an issue with our CORS set up, will update this, or hit you up on #swagger once I verify this is fixed.

fehguy commented 10 years ago

I put up a short page on handling CORS issues, you might look here for some other tips:

https://github.com/wordnik/swagger-core/wiki/CORS

df-jsykes commented 10 years ago

Turns out its not a cors issue, running everything locally, still seeing a "default callback:" wrapper, investigating. Thanks man.

df-jsykes commented 10 years ago

Its working on localhost, I wont know anything about the CORS issue til next week, but the file system / localhost stuff is working now, only it still logs the data in the console with "default wrapper", that's going to confuse some people possibly. Thanks for your help

fehguy commented 10 years ago

you'll only see that message if there is no other callback set in the library. reopen if you still have trouble