1

I'm making an ajax request to retrieve json data from webtrends - a service that requires a login. I'm passing the username and password in my ajax request, but still gives me a 401 unauthorized error. I've tried 3 different methods - but no luck. Can someone pls help me find a solution?

1.   $.getJSON('https://ws.webtrends.com/..?jsoncallback=?', { format: 'jsonp', suppress_error_codes: 'true', username: 'xxx', password: 'xxx', cache: 'false' }, function(json) {
         console.log(json);        
       alert(json);
  });


2.  $.ajax({
    url: "https://ws.webtrends.com/../?callback=?",
    type: 'GET',
    cache: false,
    dataType: 'jsonp',
    processData: false,
    data: 'get=login',
            username: "xxx",
            password: "xxx",
    beforeSend: function (req) {
        req.setRequestHeader('Authorization', "xxx:xxx");
    },
    success: function (response) {
        alert("success");
    },
    error: function(error) {
      alert("error");
    }
});

3. window.onload=function() {
var url = "https://ws.webtrends.com/...?username=xxx&password=xxx&callback=?";
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
}

function parseRequest(response) {
                try  {
alert(response);
}
catch(an_exception)  {
                    alert('error');
                }
}
  • For ***Method 2*** you should send the username / password as a base 64 encoded string. This previous SO post shows how : http://stackoverflow.com/questions/11540086/jquery-ajax-header-authorisation – AardVark71 Feb 09 '13 at 14:22

1 Answers1

0

Method 3 might work when you use a named callback function and use basic authentication in the url. Mind though that a lot of browsers don't accept url-authentication (or whatever the name is). If you want to try it, you can rewrite it like this:

window.onload = function() {
 var url = "https://xxx:xxx@ws.webtrends.com/...?callback=parseRequest";
 var script = document.createElement('script');
 script.setAttribute('src', url);
 document.getElementsByTagName('head')[0].appendChild(script);
}

function parseRequest(response) {
  try  {
     alert(response);
  }
  catch(an_exception)  {
     alert('error');
  }
}
AardVark71
  • 3,928
  • 2
  • 30
  • 50