Thursday, 15 April 2010

jsonp - 401 (Unauthorized) error with ajax request (requires username and password) -



jsonp - 401 (Unauthorized) error with ajax request (requires username and password) -

i'm making ajax request retrieve json info webtrends - service requires login. i'm passing username , password in ajax request, still gives me 401 unauthorized error. i've tried 3 different methods - no luck. can pls help me find 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) { seek { alert(response); } catch(an_exception) { alert('error'); } }

method 3 might work when utilize named callback function , utilize basic authentication in url. mind though lot of browsers don't take url-authentication (or whatever name is). if want seek it, can rewrite 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) { seek { alert(response); } catch(an_exception) { alert('error'); } }

ajax jsonp http-status-code-401

No comments:

Post a Comment