解決DWR2.0發送資料Locale為zh_C的BUG

來源:互聯網
上載者:User
在編程中,使用DWR進行AJAX互動。後台採用的是判斷request的local來選擇國際話資源,結果老是不正確,一查才發現Local竟然是zh_C。於是追溯到Servlet的doPost,還是zh_C。繼續追溯,發現是HTTPXML做POST動作時,發送的language不對。但是jquery卻能正常,比較了一下,發現DWR採用的是new ActiveXObject的方式產生的XMLHTTPRequest。而jquery是直接使用瀏覽器內建對象。難怪與通常的會不一致,也許IE的某個DOM出了問題,錯把zh_C當zh_CN來用了吧。

找到問題瞭解決起來就簡單。修改dwr的engine.js,找到以下語句:
      try {
        for (prop in batch.headers) {
          var value = batch.headers[prop];
          if (typeof value == "string") batch.req.setRequestHeader(prop, value);
        }
        if (!batch.headers["Content-Type"]) batch.req.setRequestHeader("Content-Type", "text/plain");
  }
添加修正頭部瀏覽器語言的代碼,改為這樣:
      try {
        for (prop in batch.headers) {
          var value = batch.headers[prop];
          if (typeof value == "string") batch.req.setRequestHeader(prop, value);
        }
        if (!batch.headers["Content-Type"]) batch.req.setRequestHeader("Content-Type", "text/plain");
  //fix language header bug under IE
  if(navigator.systemLanguage){
   batch.req.setRequestHeader("accept-language", navigator.systemLanguage);
  }
      }
在servlet的doPost於是就收到了正確Locale

折騰死我了,google也找不到答案,幸好開源的能夠追蹤代碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.