2013-02-09 63 views
1

我正在做一个ajax请求来从webtrends检索json数据 - 一种需要登录的服务。我在我的ajax请求中传递用户名和密码,但仍然给我401未经授权的错误。我尝试了3种不同的方法 - 但没有运气。有人可以帮我找到解决方案吗?401(未经授权)ajax请求错误(需要用户名和密码)

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'); 
       } 
} 
+0

对于***方法2 ***,你应该为基地64编码的字符串发送用户名/密码。这之前的SO贴子显示了如何:http://stackoverflow.com/questions/11540086/jquery-ajax-header-authorisation – AardVark71 2013-02-09 14:22:41

回答

0

方法3当你要用一个回调函数可能会奏效,并在URL中使用基本身份验证。不过请注意,很多浏览器都不接受url认证(或任何名称)。如果你想尝试它,你可以把它改写这样的:

window.onload = function() { 
var url = "https://xxx:[email protected]/...?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'); 
    } 
} 
+0

谢谢 - 我能够得到它在FF和Chrome,但不是IE的工作。 – user1337813 2013-02-10 05:43:51

相关问题