3

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

what I input: питання

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

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

Here's my AJAX request:

$.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.

4

2 に答える 2

4

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: encodeURIComponent("питання") returns %D0%BF%D0%B8%D1%82%D0%B0%D0%BD%D0%BD%D1%8F

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

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

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

于 2013-01-11T02:13:08.630 に答える
1

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

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

$.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.

于 2013-01-11T10:31:33.483 に答える