1
我想向使用AJAX的tomcat服務器發送DELETE請求。使用JQuery Ajax Tomcat的特殊字符的基本身份驗證
我希望請求使用基本身份驗證。如果密碼包含特殊字符,我無法真正到達那裏。
系統的配置是:
- 的jQuery 1.10.2
- 的Tomcat 7.0.47
問我嘗試按照我的發現之前在:
- How to use Basic Auth with jQuery and AJAX?
- How can you encode a string to Base64 in JavaScript?
- Javascript Ajax call using password with special characters
Tomcat的配置是:
</tomcat-users>
<role rolename="authUser"/>
<user username="username" password="password" roles="authUser"/>
</tomcat-users>
我使用jQuery
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: "username",
password: "password",
success: function(){
alert("Data Deleted");
}
});
利用這種配置一切工作做出了Ajax請求。
如果我把特殊字符放在tomcat的配置文件中,我不能再做出正確的請求。
</tomcat-users>
<role rolename="authUser"/>
<user username="username" password="AeHm#%%9Rt\'JzX^|A'N" roles="authUser"/>
</tomcat-users>
我嘗試有兩個AJAX設置
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: "username",
password: "AeHm#%%9Rt\'JzX^|A'N",
success: function(){
alert("Data Deleted");
}
});
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: encodeURIComponent("username"),
password: encodeURIComponent("AeHm#%%9Rt\'JzX^|A'N"),
success: function(){
alert("Data Deleted");
}
});
與此兩種配置我看到了同樣的要求。
所以我嘗試更較老的方法
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\'JzX^|A'N"))));
xhr.setRequestHeader("Authorization", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\'JzX^|A'N"))));
},
success: function(){
alert("Data Deleted");
}
});
有了這個請求,我總是得到錯誤:401(未授權)
但usign C#的客戶,我能夠進行身份驗證請求:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:8080/TestAuthentication");
request.Credentials = new NetworkCredential("username", @"AeHm#%%9Rt\'JzX^|A'N");
....
而且我也可以訪問URL,如果我把「username」和「AeHm#%% 9Rt \'JzX^| A'N」告訴我從瀏覽器中看到。
所以它似乎是我提出的請求的問題。
請問有人可以幫我嗎?
閱讀此問題,它與您的相關:http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax –