[英]JavaScript to PHP - POST encoding issue
I'm attempting to send a string from client-side JavaScript to a back-end PHP script. 我正在尝试将字符串从客户端JavaScript发送到后端PHP脚本。 The string contains special quotes like '
and “
. 该字符串包含特殊引号,例如'
和“
。
When I look at the console in Chrome I can see that these are sent in the POST headers as they are. 当我查看Chrome中的控制台时,可以看到它们按原样在POST标头中发送。 On the PHP side I then immediately json_encode()
the $_POST
array and send it back to see what its collected. 在PHP方面,然后我立即对$_POST
数组进行json_encode()
并将其发送回去,以查看其收集的内容。 The special characters now look like this \’
. 现在,特殊字符看起来像\’
。 This is for testing please note I would normally sanitize all post data. 这是用于测试,请注意,我通常会清理所有发布数据。
I wish to use UTF-8 but I'm not sure what I'm missing. 我希望使用UTF-8,但不确定自己缺少什么。 My HTML includes: 我的HTML包括:
<meta charset="utf-8">
My PHP server has UTF-8 set as its default charset. 我的PHP服务器已将UTF-8设置为其默认字符集。
If I start saving such data to the database I start ending up with strings like this: âÂÂ
for '
. 如果我开始这样的数据保存到我开始用这样的字符串结束数据库: âÂÂ
的'
。 However this is not a database issue the characters are already bad before going into the database. 但是, 这不是数据库问题 ,在进入数据库之前字符已经坏了。 MySQL purely accentuates them. MySQL纯粹是在强调它们。
Any ideas? 有任何想法吗?
Update 更新资料
I've noticed that if I return the string back to javascript without using json_encode()
then it's in its original format with the special quotes ( '
and “
) still. 我注意到,如果不使用json_encode()
将字符串返回到javascript,则它的原始格式仍带有特殊引号( '
和“
)。
Have you tried: 你有没有尝试过:
utf8_decode()
On the server side for the variables you're passing? 在服务器端您要传递的变量? PHP is likely expecting iso-8859-1 rather than uft-8 . PHP可能期望的是iso-8859-1而不是uft-8 。
Turns out there was an issue both sides of the pond. 事实证明,池塘两边都有问题。 The issue PHP side which this question regards was that the data was being sent to the back-end via a GET request (url encoded). 这个问题涉及的PHP问题是数据是通过GET请求(URL编码)发送到后端的。 I have changed this to a POST request. 我已将其更改为POST请求。
This has allowed me to specify the UTF-8 charset when sending the headers for the POST request. 这使我可以在发送POST请求的标头时指定UTF-8字符集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.