如何发送特殊字符到php服务器

时间:2018-08-14 17:26:15

标签: php angular post character

我正在将特殊字符发送到php服务器。我尝试使用angular和jquery获得相同的结果:

带有jQuery的myClient.js:

data = "Ñoñón";
dataToSend={special_word: data};
$.ajax({    
  type : 'POST',
  url  : 'myServer.php',
  data : dataToSend,
  success :  function(response){
    console.log(response);
 }
});

myServer.php for JQuery客户端:

$special_word =mysqli_real_escape_string($mysqli, $_POST["special_word"]));
saveWordInDB($special_word);
echo $special_word;

myClient.js与角度:

data = "Ñoñón";
dataToSend={special_word: data};
$http.post('myServer.php', dataToSend)
.then(
   function(response){
     // success callback
     console.log(response.data);
   }, 
   function(response){
     // failure callback
     console.log(response);
   }
);

用于角度客户端的myServer.php

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$special_word = $request->special_word;
saveWordInDB($special_word);
echo $special_word;

在两种情况下,单词“Ñoñón”都没有存储在数据库中,而是单词“Ãoo±n”存储在数据库中。控制台中显示的单词也一样。如何解决?

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您在这里遇到utf8_decode问题,请先对其进行解码,然后再保存到数据库中

$special_word 
=mysqli_real_escape_string($mysqli, 
$_POST["special_word"]));
saveWordInDB(utf8_decode($special_word));
echo $special_word;

参考 http://php.net/manual/en/function.utf8-decode.php