1

I am trying to hit API in javascript but getting error code 401 but on postman it is working fine.

Here is my javascript code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>ABC</title>
    <script src="jquery-3.1.0.min.js"></script>
    </head>

        <body onload="test()"> </body>

        <script type="text/javascript">

        function test()
              {
                var credential = '[{"id":"recording", "userId":"administrator", "password":"Expert111"}]'
                $.ajax({
                      url: "http://xx.xx.xx.xx/api/rest/authorize",
                      type: "POST",
                      data: { id: "recording", userId: "administrator", password: "Expert111" },
                       dataType: "jsonp",
                            success: function (result) {
                                switch (result) {
                                    case true:
                                        alert("Success");
                                        break;
                                    default:
                                        alert("fail:"+result);
                                }
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                            alert("status: " +xhr.status +", ajaxOptions:"+ajaxOptions +",Error:" +thrownError);
                            },
                            //data : credential
                        });
                    };
    </script>
    </html>

following is postman snapshot (working fine)

enter image description here

following is result of request via code:

enter image description here

enter image description here

Touqeer
  • 77
  • 9
  • 1
    I notice in your *postman* data you don't include the `callback` or `_` parameters - so, you're not really testing the same thing – Jaromanda X Sep 18 '16 at 05:11
  • Can you please guide me to include callback or _ parameters? – Touqeer Sep 18 '16 at 05:12
  • Though code seems fine, kindly provide the url so we can try at our end too by running it or set up a dummy url in case you don't wanna share the original. – MGA Sep 18 '16 at 05:14
  • 1
    I don't know `postman` ... how did you add id, userId and password parameters to the request? ... I think I see an issue ... your `postman` actually does POST the parameters, whereas your ajax is doing a GET request because that's how JSONP is done – Jaromanda X Sep 18 '16 at 05:15
  • see http://stackoverflow.com/questions/4508198/how-to-use-type-post-in-jsonp-ajax-call – Jaromanda X Sep 18 '16 at 05:16
  • clearly, the API at `http://xx.xx.xx.xx/api/rest/authorize` doesn't accept a GET request, only a POST - therefore you can not use JSONP - and if they don't use CORS, then you wont be able to use JSON datatype with POST method either - you'll need to "proxy" the request through your own site and have your server make a proper POST request to `http://xx.xx.xx.xx/api/rest/authorize` – Jaromanda X Sep 18 '16 at 05:19
  • if i send JSON in the request, i am getting this error: Failed to load resource: the server responded with a status of 400 (Bad Request) example.html:1 XMLHttpRequest cannot load http://xx.xx.xx.xx/api/rest/authorize. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 400. – Touqeer Sep 18 '16 at 05:19
  • @Touqeer - see my 2 comments just above yours - it turns out CORS is an issue after all – Jaromanda X Sep 18 '16 at 05:20

1 Answers1

0

I don't know how your server processing authorization, but looks like you didn't pass it. 401 means - Unauthorized: Access is denied due to invalid credentials. To my mind, you have to specify Authorization header.

var ah = "Basic "+atob("administrator"+":"+"Expert111");

And set this as Authorization header like there.

Moreover, you could check more information about basic authentication and encoding to base64

If it isn't help, you could check with fiddler, what your postman sends to the server and implement your request in the same way.

Community
  • 1
  • 1
RredCat
  • 5,259
  • 5
  • 60
  • 100