0

I'm using "reqwest" to make rest call to service. I'm interested in the error that occur while making a call. Here is my simple exmple

<!doctype html>
<html>
<body>
<script src="node_modules/es6-promise/dist/es6-promise.js"></script>
<script src="node_modules/reqwest/reqwest.js"></script>
<script src="src/js/test.js"></script>

</body>
</html>

Here is my test.js

reqwest({
    url: "www.google.com"
    , type: 'json'
    , method: 'post'
    , contentType: 'application/json'
    , headers: {
        'X-My-Custom-Header': 'SomethingImportant'
    }
}).then((res)=> {
    alert(res);
}, (err)=> {
    alert(err);
});

The following error is printed on my console.

XMLHttpRequest cannot load http://www.google.com/. 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:63342' is therefore not allowed access. The response had HTTP status code 405.

I want to capture this information, but the call back function on failure does not capture this information in err param. It is empty XMLHTTPRequest. I am sure it does not have any other parameters. Never the less the request cannot be empty. What am I doing wrong.

Cœur
  • 37,241
  • 25
  • 195
  • 267
FourOfAKind
  • 2,298
  • 5
  • 31
  • 34
  • This appears to be a problem with CORS. You're using XHR to request data from another origin (domain) than the one your site originally loaded from. You'll never see content on such requests--your browser blocks them for security reasons. Read more here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS – vastlysuperiorman Feb 10 '16 at 03:01
  • I agree with you. There is no way I can capture that information in java script? At lease how do I know which script and which line responsible for printing this line? I tried putting break points in the script looks like default try catch method on the promise gets called. – FourOfAKind Feb 10 '16 at 03:16
  • Hmm... I see the problem and have replicated here. Odd that after the error the XHR object contains no information on the failure. – vastlysuperiorman Feb 10 '16 at 03:31
  • ya exactly. Given the popularity of the library, some one should have reported it. What I'm looking for is pretty basic I guess. Even though my knowledge on JS is limited. – FourOfAKind Feb 10 '16 at 03:51
  • looks like some one reported here https://github.com/ded/reqwest/issues/230 – FourOfAKind Feb 10 '16 at 03:55

0 Answers0