我写了 jQuery post 来向服务器发送请求,让客户知道他们的名字以该请求开头,但问题是它只发送英文字符 这是邮政编码
console.log("1 - "+inputString); //return Arabic right
console.log("2 - "+decodeURIComponent(inputString)); //return arabic right
console.log("3 - "+encodeURIComponent(inputString)); //encoded Arabic
inputString = decodeURIComponent(inputString);
console.log(inputString); //also arabic right
$.post("<?= base_url(); ?>admin/customers/get_customer/"+inputString, function(data){
alert(data);
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
在服务器端
function get_customer($customer)
{
header ('Content-type: text/html; charset=utf-8');
die($customer." - - ".utf8_decode($customer)." - - ".utf8_encode($customer)); //just for testing and all returns encoded characters
$query = mysql_query("SELECT * FROM customers WHERE customer_name LIKE '$customer%' LIMIT 10") or die(mysql_error());
$str = "";
while ($result = mysql_fetch_object($query) ):
$str .= '<li onClick="fill(\''.$result->customer_id.'\',\''.$result->customer_name.'\');">'.$result->customer_name.' </li>';
endwhile;
echo $str;
}
View 文件中的字符集是 HTML & PHP 的 utf-8 另外 jquery 的字符集是 UTF-8 那么问题是什么
最佳答案
我认为问题中的示例和您的解决方案都没有完全展示这两个部分。 URL 只能包含 ASCII 字符,因此唯一的选择是在发送请求之前对阿拉伯语部分进行编码。在构建您的 URL 时,您需要这样的东西:
$.post("<?= base_url(); ?>.../"+encodeURIComponent(inputString), function(data){
// ...
});
您的解决方案显示的第二部分是在服务器端解码数据:
function get_customer($customer) {
$customer = urldecode($customer);
// ...
}
关于php - AJAX 请求不发送阿拉伯字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8273788/