Encoding Problem In AJAX

2019-04-17 00:07发布

I'm trying to create two drop down list in a form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list options by the selected value of first drop down list from MySQL database. I did this by using the code from this link (AJAX)

http://www.w3schools.com/PHP/php_ajax_database.asp

My problem that the page called by javascript can't be encoded. Besides I'm using the Arabic lang so the outcome of the second drop down menu is meaningless shapes.

I have tried different ways to solve the problem like using header(..),AJAX.get(..) but NO one works :(

how can i solve this problem?

Thanks in Advance!! Regards, Manalkk

4条回答
叛逆
2楼-- · 2019-04-17 00:10

on the back end try using utf8_encode()

查看更多
迷人小祖宗
3楼-- · 2019-04-17 00:11

One way is to exchange between cliend and server by only encoded messages. If both request and response contains special chars then you can do:

  • Encode ajax request
  • Decode request, form a response and encode it before sending
  • Decode response on client side

I often use base64 encoding to avoid charset problems, but you can use anything else.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-04-17 00:11

Try setting your database connection to;

mysql_query('SET NAMES "utf8"');
查看更多
干净又极端
5楼-- · 2019-04-17 00:25

The browser till interpret it based on the character set that you specify in the http headers. That requires that the actual output that you are sending is following that encoding.

With your setup the following must be true:

You must fetch all data from mysql on a connection that is specified with the correct encoding (SET NAMES "encoding"), the default is latin1 / ISO-8859-1

PHP internally only work with latin1 as well, but any dynamic data should be okay unless you don't do a lot of string manipulation, then you might run into problem.

If any content in your php-page is static, you must save the php-page in the corresponding charset (for example, utf8).

The php page must specify content type in the header:

header("Content-Type: text/html;charset=utf8"); //replace with the actual content type and charset you are using.

Your data in the database must be stored accordingly, from a connection that has specified the correct encoding, otherwise the database will do incorrect conversion to or from the database.

查看更多
登录 后发表回答