AJAX将cyrillic转换为某种编码

时间:2022-08-23 08:06:14

I have a problem. After submission of a form I send request using AJAX. Everything was perfect until I tried Cyrillic text.

我有一个问题。提交表单后,我使用AJAX发送请求。一切都很完美,直到我尝试了西里尔文。

what I input: питання

我输入:питання

what alerts me javascript: питання

提醒我javascript:питання

what echos me $_POST['question']: %u041F%u0438%u0442%u0430%u043D%u043D%u044F

什么声音在我耳边回响['问题']:%u041F%u0438%u0442%u0430%u043D%u043D%u044F

Here's my AJAX request:

这是我的AJAX请求:

$.ajax({
        type: "POST",
        url: "addQuestion.php",
        data: "u_id=" + $("#u_id").val() + "&u_a_name=" + $("#u_a_name").val() + "&question="+escape($("#question_input").val()),
        success: function(data) {
                if (data == "Asked") {
                alert("Asked");
                window.location.reload();
            } else {
                alert(data);
            }
        }
    });

So I thought it is AJAX problem, but I haven't found answer in internet. Thank you for attention.

所以我认为这是AJAX的问题,但是我还没有在互联网上找到答案。谢谢你的关注。

2 个解决方案

#1


2  

Javascript's escape() doesn't work too well with non-ASCII characters, and to handle any unicode characters I generally use encodeURIComponent() instead. In PHP, you can use urldecode() to reverse the same encoding. So:

Javascript的escape()对于非ascii字符不太适用,对于我通常使用的encodeURIComponent()来处理任何unicode字符,Javascript的escape()都不太适用。在PHP中,可以使用urldecode()反转相同的编码。所以:

Javascript: encodeURIComponent("питання") returns %D0%BF%D0%B8%D1%82%D0%B0%D0%BD%D0%BD%D1%8F

Javascript:encodeURIComponent(питання)返回% D0%BF d0%b0%d0%bd % D1 D0%BD % % % D0%B8%D1 % 82% 8 f

PHP: urldecode("%D0%BF%D0%B8%D1%82%D0%B0%D0%BD%D0%BD%D1%8F"); returns питання

PHP:urldecode(“% D0%BF d0%b0%d0%bd % D1 D0%BD % % % D0%B8%D1 % 82% 8 f”);返回питання

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent

http://php.net/manual/en/function.urldecode.php

http://php.net/manual/en/function.urldecode.php

#2


1  

escape uses non-standard URL encoding and shouldn't be used at all.

转义使用非标准URL编码,根本不应该使用。

Anyway, with jQuery, you wouldn't put your data as string but as an object and let jQuery format and encode it:

不管怎样,使用jQuery,你不会将数据作为字符串,而是作为对象,让jQuery格式化并对其进行编码:

$.ajax({
        type: "POST",
        url: "addQuestion.php",
        data: {
            u_id: $("#u_id").val(),
            u_a_name: $("#u_a_name").val(),
            question: $("#question_input").val()

        },

        success: function(data) {
                if (data == "Asked") {
                alert("Asked");
                window.location.reload();
            } else {
                alert(data);
            }
        }
});

Much cleaner and easier.

更清洁和更容易得多。

#1


2  

Javascript's escape() doesn't work too well with non-ASCII characters, and to handle any unicode characters I generally use encodeURIComponent() instead. In PHP, you can use urldecode() to reverse the same encoding. So:

Javascript的escape()对于非ascii字符不太适用,对于我通常使用的encodeURIComponent()来处理任何unicode字符,Javascript的escape()都不太适用。在PHP中,可以使用urldecode()反转相同的编码。所以:

Javascript: encodeURIComponent("питання") returns %D0%BF%D0%B8%D1%82%D0%B0%D0%BD%D0%BD%D1%8F

Javascript:encodeURIComponent(питання)返回% D0%BF d0%b0%d0%bd % D1 D0%BD % % % D0%B8%D1 % 82% 8 f

PHP: urldecode("%D0%BF%D0%B8%D1%82%D0%B0%D0%BD%D0%BD%D1%8F"); returns питання

PHP:urldecode(“% D0%BF d0%b0%d0%bd % D1 D0%BD % % % D0%B8%D1 % 82% 8 f”);返回питання

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent

http://php.net/manual/en/function.urldecode.php

http://php.net/manual/en/function.urldecode.php

#2


1  

escape uses non-standard URL encoding and shouldn't be used at all.

转义使用非标准URL编码,根本不应该使用。

Anyway, with jQuery, you wouldn't put your data as string but as an object and let jQuery format and encode it:

不管怎样,使用jQuery,你不会将数据作为字符串,而是作为对象,让jQuery格式化并对其进行编码:

$.ajax({
        type: "POST",
        url: "addQuestion.php",
        data: {
            u_id: $("#u_id").val(),
            u_a_name: $("#u_a_name").val(),
            question: $("#question_input").val()

        },

        success: function(data) {
                if (data == "Asked") {
                alert("Asked");
                window.location.reload();
            } else {
                alert(data);
            }
        }
});

Much cleaner and easier.

更清洁和更容易得多。