2

I've racked my brain over this and I cant seem to figure out the solution so ask humbly for your assistance please. I'm using the restlet client on chrome which uses a cURL command to GET json from a server. I need to convert this curl command over to AJAX, and I'll provide as much code as I'm allowed to. Could you please help me by converting this over to jQuery's Ajax?

cURL Command: 

curl -i -L -X GET \
   -H "Authorization:Basic base64username:base64password" \
 'https://internalserverurl'

Here's what I have for my jQuery AJAX Method:

function make_base_auth(user, password) {
            var tok = user + ':' + password;
            var hash = Base64.encode(tok);
            return "Basic " + hash;
        }

        function getData() {
            $.ajax({
                url: "https://internalurl",
                beforeSend: function(xhr) { 
                  xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password")); 
                },
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                processData: false,
                success: function (data) {
                  alert(JSON.stringify(data));
                    console.log(data);
                },
                error: function (data) {
                    var strBuilder = [];
                    for(key in data){
                          if (data.hasOwnProperty(key)) {
                             strBuilder.push(key + ":" + data[key] + "<br><br>");
                        }
                    }
                    alert(strBuilder.join(""));
                      $('#errordiv').html(strBuilder);
                }
            });
        }

Thank you all for your assistance.

4
  • I see no actual question here. Commented Jun 1, 2017 at 14:54
  • You're correct @CBroe thanks for that catch. Apologies. Commented Jun 1, 2017 at 14:57
  • You can use the $.ajax method. Follow include your success callback method in the success block. Commented Jun 1, 2017 at 14:57
  • jQuery.ajax parameters username and password take care of the auth part. Commented Jun 1, 2017 at 14:58

1 Answer 1

1

I said it in the comments, but you can use the $.ajax function and set your headers in the beforeSend block.

    $.ajax({
      url: 'https://internalserverurl',
      type: 'GET',
      dataType: 'json',
      success: function() { alert('hello!'); },
      error: function() { alert('boo!'); },
      beforeSend: setHeader
    });
  });

  function setHeader(xhr) {
    xhr.setRequestHeader('Authorization':'Basic base64username:base64password');

  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much @franzke !!! This works! Now I just have to get it to work outside of IE and I'll be golden (cert is self signed so modern browsers obviously shut it down).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.