Php AJAX请求不发送阿拉伯字符
我写jquerypost是为了向服务器发送请求,让客户知道他们的名字以该请求开头,但问题是它只发送英文字符 这是邮政编码Php AJAX请求不发送阿拉伯字符,php,javascript,jquery,character-encoding,Php,Javascript,Jquery,Character Encoding,我写jquerypost是为了向服务器发送请求,让客户知道他们的名字以该请求开头,但问题是它只发送英文字符 这是邮政编码 console.log("1 - "+inputString); //return Arabic right console.log("2 - "+decodeURIComponent(inputString)); //return arabic right console.log("3 - "+encodeURIComponent(inputString)); //enco
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;
}
视图文件中的字符集是HTML和PHP的utf-8
jquery的字符集也是UTF-8
那么问题出在哪里我已经解决了,这是我的解决方案
function get_customer($customer)
{
header ('Content-type: text/html; charset=utf-8');
$customer = urldecode($customer);//this is the solution decode the url
$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;
}
这有两个部分,我认为问题中的例子和你的解决方案都没有充分说明。URL只能包含ASCII字符,因此唯一的选择是在发送请求之前对阿拉伯语部分进行编码。在构建URL时,您需要以下内容:
$.post("<?= base_url(); ?>.../"+encodeURIComponent(inputString), function(data){
// ...
});
我找到了解决办法,问题是主机没有通知我MySQL服务器升级。所以基本上我是用这些查询连接到数据库的
mysql_query("SET NAMES 'utf8'",$dbhandle);
mysql_query("SET CHARSET 'utf8'",$dbhandle);
然后使用此函数而不是上一个函数
mysql_set_charset("utf8", $dbhandle);
我解决了这个问题
mysql_set_charset("utf8", $dbhandle);