2014-02-20 23 views
1

我想向使用AJAX的tomcat服務器發送DELETE請求。使用JQuery Ajax Tomcat的特殊字符的基本身份驗證

我希望請求使用基本身份驗證。如果密碼包含特殊字符,我無法真正到達那裏。

系統的配置是:

  • 的jQuery 1.10.2
  • 的Tomcat 7.0.47

問我嘗試按照我的發現之前在:

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」告訴我從瀏覽器中看到。

所以它似乎是我提出的請求的問題。

請問有人可以幫我嗎?

+0

閱讀此問題,它與您的相關:http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax –

回答

1

最後我不得不在已瞭解問題是:在逃生

寫作:

  • AeHm#%% 9RT \'JZX^| A'N
  • 錯誤輸出:AeHm#% %9Rt'JzX^| A'N =>讓我失去了 「\」 字符

編寫密碼轉義字符 「\」

  • AeHm#%% 9RT \\ 'JZX^| A'N
  • 正確的輸出:AeHm#%% 9RT \' JZX^| A'N

這裏的工作代碼:

$.ajax({ 
    type: "DELETE", 
    url: "http://localhost:8080/TestAuthentication", 

    beforeSend: function(xhr) { 
     //May need to use "Authorization" instead 
     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"); 
    } 
});