简体   繁体   English

使用Ajax在Oracle中存储汉字不起作用

[英]Storing chinese character in oracle using ajax is not working

I want to store chinese characters from an html page into oracle database using ajax concept. 我想使用ajax概念将html页面中的汉字存储到oracle数据库中。

Front end : HTML & PHP.
Back end  : Oracle 11G.

Oracle Characteristics: Oracle特点:

NLS_LANGUAGE = AMERICA
NLS_CHARACTERSET = WE8MSWIN1252
NLS_NCHAR_CHARACTERSET = AL16UTF16

when I try to store chinese character using form submit it's storing successfully, if I try to store through ajax I am getting weird characters in the database. 当我尝试使用表单提交存储汉字时,它存储成功,如果我尝试通过ajax存储,我在数据库中得到了奇怪的字符。

In the ajax page I have added this line: 在ajax页面中,我添加了以下行:

header("Content-type: text/html; charset=utf-8");

and in the html page added the below line: 并在html页面中添加以下行:

<meta http-equiv="Content-type" value="text/html; charset=utf-8">

I have gone through many stack overflow suggestions but no luck. 我经历了很多堆栈溢出建议,但是没有运气。

Please advice me on how to solve this. 请给我建议如何解决这个问题。

Thanks in advance. 提前致谢。

Code: HTML Code. 代码: HTML代码。

<html>
  <head>
    <meta http-equiv="Content-type" value="text/html; charset=utf-8">
    <title>Test</title>
    <script src="jquery.min.js"></script>
    <script>
    function addForm()
    {
      var english=document.getElementById('txt_english').value;
      var id=document.getElementById('txt_id').value;
      var chinese=document.getElementById('txt_chinese').value;
      $.ajax({
          type: "POST",
          async: false,
          url: "ajax/ajax_add_form.php",
          data:
          {
            english: english,
            chinese: chinese,
            id: id
          }
        }).done(
        function (html){
            alert(html);
        });
      }
    </script>
  </head>
  <body>
    <input type='text' name='txt_id' id='txt_id' value='' /><br>
    <input type='text' name='txt_english' id='txt_english' value='' />                          
    <input type='text' name='txt_chinese' id='txt_chinese' value='' /> 
    <button type="button" onclick="addForm()">Click Me!</button> 
  </body>
</html>

PHP Code: ajax/ajax_add_form.php PHP代码: ajax / ajax_add_form.php

<?php
header("Content-type: text/html; charset=utf-8");
include("../config.php");
extract($_REQUEST);
$sql=oci_parse($conn,"insert into  test(id,english,chinese1)values(:id,:english,:chinese)");
oci_bind_by_name($sql, ':id', $id); 
oci_bind_by_name($sql, ':english', $english); 
oci_bind_by_name($sql, ':chinese', $chinese);
oci_execute($sql);
echo "success";
?>

在此处输入图片说明

My Issue: 我的问题:

Even though it stores in another format when i get back in web page its working and displays correctly. 即使当我返回网页时它以另一种格式存储时,它的工作原理并正确显示。

My problem is another tool called agile is also accessing the same data , hence there conversion is not happening it displays what ever data format is stored in database. 我的问题是另一个称为敏捷的工具也正在访问相同的数据,因此不会发生转换,因为它显示了存储在数据库中的数据格式。

Hence i want to store exactly as user enters , no need of any conversion. 因此,我想完全按照用户输入的内容进行存储,而无需进行任何转换。

Ensure that you've set your OCI connection to use UTF-8: 确保已将OCI连接设置为使用UTF-8:

oci_connect($username, $password, $connection_string, 'UTF8');

This tells Oracle the charset encoding you are sending and expect to receive. 这告诉Oracle您正在发送并希望接收的字符集编码。 Oracle then takes care of 'translating' between the server and the client automatically. 然后,Oracle会自动在服务器和客户端之间进行“转换”。 From the documentation (emphasis mine): 文档 (重点是我的):

Determines the character set used by the Oracle Client libraries. 确定Oracle客户端库使用的字符集。 The character set does not need to match the character set used by the database. 字符集不需要与数据库使用的字符集匹配。 If it doesn't match, Oracle will do its best to convert data to and from the database character set . 如果不匹配,Oracle将尽最大努力在数据库字符集之间来回转换数据 Depending on the character sets this may not give usable results. 根据字符集,这可能不会给出可用的结果。 Conversion also adds some time overhead. 转换也增加了一些时间开销。

The only thing you then need to take care of is that your PHP application treats everything as UTF-8. 您唯一需要注意的是,您的PHP应用程序将所有内容都视为UTF-8。

Try 尝试

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

instead of 代替

<meta http-equiv="Content-type" value="text/html; charset=utf-8">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM