Amareis / another-rest-client

Simple pure TypeScript REST API client that makes your code lesser and more beautiful than without it.
MIT License
174 stars 16 forks source link

Error running simple test #6

Closed abnerjacobsen closed 7 years ago

abnerjacobsen commented 7 years ago

First of all I am not a javascript programmer.

I'm trying to make a simple call to a remote API on one of my servers. The code is below:

<html>
<head>
    <script src="rest-client.js"></script>
    <script>
        window.onload = function () {
            var api = new RestClient('https://api.asscontal.d.datanuvem.com/1.0');
            api.on('request', function(xhr) {
                xhr.setRequestHeader('Authorization', 'someapikey');
            });
            api.res({sites: 'sites'});

            api.sites.get().then(function(k){alert(k)});
        }
    </script>
</head>
<body>
</body>
</html>

However, the browser reports the following errors:

XMLHttpRequest cannot load https://api.asscontal.d.datanuvem.com/1.0/sites. Response for preflight is invalid (redirect) rest-client.js:167 XHR failed loading: GET "https://api.asscontal.d.datanuvem.com/1.0/sites". _request @ rest-client.js:167 self.get @ rest-client.js:236 window.onload @ daspanel.html:12 daspanel.html:1 Uncaught (in promise) XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: function…}

The copy of res-client.js I'm using was obtained from this URL: https://raw.githubusercontent.com/Amareis/another-rest-client/master/rest-client.js

What am I doing wrong ?

Thank you.

Amareis commented 7 years ago

Firstly, I got a browser warning when trying open your API by link, may be you have some problem with HTTPS SSL certificate. Secondly, see this: https://stackoverflow.com/questions/33645511/why-my-ajax-showing-preflight-is-invalid-redirect-error

Amareis commented 7 years ago

Thirdly, when I'm skipping warning, site redirect me to another site, and this is root of the evil, because XMLHTTPRequest cannot into redirects on another domains.

abnerjacobsen commented 7 years ago

Thanks, helped a lot.