Microsoft Edge ajax 奇怪的 POST 数据编码

Microsoft Edge ajax weird POST data enconding

我正在使用 CodeIgniter(PHP 7.1.1) 开发一个网站,在客户端,我需要向服务器发送一些日期:

var date_str = date.toLocaleDateString();
var time_str = date.toLocaleTimeString().split(':');
time_str = time_str[0] + ':' + time_str[1];
...
data_to_ajax = {
    date: inicio,
    date2: termino
};
...

服务器将使用以下方法验证这些日期:

DateTime::createFromFormat('d/m/Y H:i', $date);
DateTime::createFromFormat('d/m/Y H:i', $date2);

它工作正常,除了使用 Microsoft Edge 时,createFromFormat 对于发送日期总是 return false。为了调试,我在发送的日期上调用了 var_dump,我得到的是:

string(43) "29/08/2017 21:00"
string(31) "29/08/2017 23:30"

在 chrome 和 mozilla 中,我得到的是:

string(16) "29/08/2017 21:00"
string(16) "29/08/2017 23:30"

好了,调试发送的 http body 数据,chrome mozilla 将发送 63 个字节:

date_loc=29%2F08%2F2017+21%3A00&date_ter=29%2F08%2F2017+23%3A30

while edge 发送 189(63*3) 字节:

date_loc=%E2%80%8E28%E2%80%8E%2F%E2%80%8E08%E2%80%8E%2F%E2%80%8E2017+%E2%80%8E21%E2%80%8E%3A%E2%80%8E00%E2%80%8E&date_ter=%E2%80%8E28%E2%80%8E%2F%E2%80%8E08%E2%80%8E%2F%E2%80%8E2017+23%3A30

两个好像都对,因为这个:url encoder/decoder可以解码,问题好像出在服务器上,为什么php显示两个相同字符数的字符串有不同的长度?更奇怪的是var_dump可以正常显示那些字符串,但是createDateFromFormat会失败。

Microsoft Edge 将 %E2%80%8E 放入发送的数据中,这是一个 Left-to-right mark, this is causing problems with the php function: createDateFromFormat, because the string sent by Edge don't fit in the format specified: d/m/Y H:i, what you can do is use the *(doc) character in the format string: *d*/*m*/*Y *H*:*i* or you can just remove all control characters(Remove control characters from php String) 使用:

$date = preg_replace('/[^\PC\s]/u', '', $date);