2012-10-14 55 views
1

我有一個是從URL參數捕獲一個變量,並通過它在過進一步槽一個html edditbale文本字段的Jquery/Ajax和然後到被傳遞給它的外部PHP代碼的PHP代碼通過我的Firebird數據庫。 問題是,jquery/ajax正在將像ä這樣的字符變成ä等等。Jquery的AJAX傳遞變量在ISO8859_1

首先我想我不得不編碼具有ISO8859_1(該表是使用)比特火鳥SQL UPDATE然後我發現了它的被改變的字符Jquery的。

這裏是我的代碼:

?> 
    <div id="wrap"> 
     <h3>Comment</h3> 
     <div id="status"></div> 
     <div id="content"> 
     <div id="editable" contentEditable="true"> 
<?php 
     echo $row[21]; 
     ?> 
</div> 
     <button id="save">Save</button> 
     </div> 
    </div> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script> 
    <script> 
    $(document).ready(function() { 
     $("#save").click(function (e) {   
      var content = $('#editable').html() 
      var nr = <?php echo $s; ?>; 
      $.ajax({ 
       url: 'save.php', 
       type: 'POST', 
       data: { 
       content: content, 
       nr: nr 
       },  
       success:function (data) {    
        if (data == '1') 
        { 
         $("#status") 
         .addClass("success") 
         .html("Data saved successfully") 
         .fadeIn('fast') 
         .delay(3000) 
         .fadeOut('slow'); 
        } 
        else 
        { 
         $("#status") 
         .addClass("error") 
         .html("An error occured, the data could not be saved") 
         .fadeIn('fast') 
         .delay(3000) 
         .fadeOut('slow'); 
        } 
       } 
      });  
     });  
     $("#editable").click(function (e) { 
      $("#save").show(); 
      e.stopPropagation(); 
     }); 
     $(document).click(function() { 
      $("#save").hide(); 
     }); 
    }); 
</script> 
+0

我做到了我的方法就是:廣告這要得到submittetd到數據庫中的最後一個變量:的iconv(「ISO-8859-1」,「UTF-8」,$內容);這是開放的最後一步:如何提交它在字符集ISO-8859-1 ??? –

回答

0

jQuery.ajax documentation;

默認[內容類型]是 「應用/的X WWW的形式,進行了urlencoded;字符集= UTF-8」

換句話說,什麼你要張貼到你的PHP頁面是一個UTF-8編碼字符串。如果你想要其他東西(比如ISO-8859-1),你需要轉換它。

+0

我該如何改變?我已經嘗試了幾件事,沒有運氣 –

+0

@MichaelMüller據我所知(如果我是我肯定我會被證明是錯誤的:)),你不能(可移植地)改變字符集'jQuery.ajax ()'在帖子中使用,你將不得不將其轉換爲服務器端。 –

+0

Btw。我也有問題,它張貼線突變
這應該是首先
但實際上真正的換行符在我的firebird分貝,如何解決這個呢? –

0

試試吧

$.ajax({ 
     data: parameters, 
     type: "POST", 
     url: ajax_url, 
     timeout: 20000, 
     contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15", 
     dataType: 'json', 
     success: callback 
}); 

你也必須在服務器上指定的字符集。再次

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?> 

更新回答您的評論

請參見下面的網址後,我想對你很重要。

如何轉碼JavaScript字符串爲ISO-8859-1

How do I transcode a Javascript string to ISO-8859-1?

+0

,沒有爲我工作,我沒有把內容類型字符串unter郵政行,但我在哪裏把php的字符集?這是傳遞錯誤字符串的文本:var content = $('#editable')。html() –

+0

我已更新我的答案。我提到了一個URL。請看一下。 –

+0

我之前讀過,沒有更進一步。問題是我對PHP和SQL有一個非常小的基本理解,但絕對沒有jqery技能。 –

0

至於說約阿希姆伊薩克森,你不能在另一個字符集提交數據比UTF-8。

我通常使用這個功能在我的前端控制器把所有$_POST陣列。

<?php 
public function direct($POST) 
{ 
      if (!empty($_POST)) { 

      $flag_unicoded = false; 

      if (strpos(strtolower($_SERVER['CONTENT_TYPE']), 'charset=utf-8') !== false) $flag_unicoded = true; 
      if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && strpos(strtolower($_SERVER['CONTENT_TYPE']), 'charset') === false) $flag_unicoded = true; 

      if ($flag_unicoded) { 
       function utf8_decode_recursive($input) { 
       $return = array(); 
       foreach ($input as $key => $val) { 
        if (is_array($val)) $return[$key] = utf8_decode_recursive($val); 
        else $return[$key] = utf8_decode($val); 
       } 
       return $return;   
       } 
       $_POST = utf8_decode_recursive($_POST); 
      } 
      return $_POST; 
      } 
} 
?> 

編輯

沒有測試,但你可以試試這個:

jQuery.ajaxSetup({'beforeSend' : function(xhr) {xhr.overrideMimeType('charset=ISO-8859-1'); }}); 
1

我解決了這個只需使用PHP翻譯前值發送到DB:

$ content2 = utf8_decode($ content);

0

我希望這不會出現重複的答案。 我有一個jsp應用程序的這個問題。該頁面在iso-8859-1中,但使用jquery.serialize()時,從IE發佈時數據會被破壞。 只見阿比德侯賽因的建議的上方,但是它改變爲

的contentType: 「應用程序/ X WWW的窗體-urlencoded;字符集= UTF-8」,

例如。告訴服務器它是UTF-8來的,而不是iso-8859-1 這解決了我的問題。

$("#caleditform").submit(function(){ 
$.ajax(
{ 
url:$("#caleditform").attr('action'), 
type: "POST", 
contentType: "application/x-www-form-urlencoded;charset=UTF-8", 
data: $('#caleditform').serialize(), 
success:function(html){ 
    $('#projectdialog').html(html); 
} 
} 
); 
return false; 

});

謝謝