accent ajax encoding issue

对着背影说爱祢 提交于 2019-12-20 03:08:43

问题


Source file has:

header('Content-type: text/html; charset=iso8859-1');

Source ajax (jQuery) script is:

$(document).ready(function() {
$.ajaxSetup({
    cache: false
});

$("#searchfield").keyup(function(){
    $("#insert_search")
        .load('ajax/searchobjects.php', {search_word:   $("#searchfield").val()}, function(){
        });
    });
});

Destination file:

header('Content-type: text/html; charset=iso8859-1');

echo $_POST['search_word'];

Data sent:

é

Result is:

é

All files:

Western (ISO Latin 1) (using TextWrangler)

Funny thing: I can insert records into MySQL just fine with accents.


回答1:


This is because you are displaying UTF-8 encoding of é (0xc3, 0xa9) as Latin-1. So the search_word was encoded as UTF-8 when it posted to PHP.

Try this,

$.ajaxSetup({
        scriptCharset: "iso-8859-1",
        cache: false
});



回答2:


That is because the default return type of an AJAX call is UTF-8. Try

utf8_encode($output);

in your ajax snippet. Alternatively, you can change the encoding of the AJAX request as described here.



来源:https://stackoverflow.com/questions/1904119/accent-ajax-encoding-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!