AJAX请求返回UNI code字符为问号 [英] AJAX request returning unicode characters as question marks

查看:718
本文介绍了AJAX请求返回UNI code字符为问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的PHP脚本被称为AJAX:

I have the following PHP script being called by AJAX:

<?php
    // file /ajax/loopback.php
    $fp = fopen("php://input","r");
    $pdt = "";
    while(!feof($fp)) $pdt .= fgets($fp);
    fclose($fp);
    $_POST = json_decode($pdt,true);
    if( !$_POST) $_POST = Array();

    var_dump($_POST);
    exit;
?>

我然后调用用下面的JavaScript此脚本:

I then call this script with the following JavaScript:

AJAX = function(url,data,callback) {
    var a = new XMLHttpRequest();

    if( data) {
        data = JSON.stringify(data);
    }

    a.open("POST","/ajax/"+url,true);
    a.onreadystatechange = function() {
        if( a.readyState == 4) {
            if( a.status == 200) {
                callback(JSON.parse(a.responseText));
            }
        }
    };
    a.setRequestHeader("Content-Type","application/json");
    a.send(data);
};

AJAX("loopback.php",{name:'ヴィックサ'},alert);

预期的结果是包含一个提示框:

The expected result is an alert box containing:

Array(1) {
    ["name"]=>
    string(5) "ヴィックサ"
}

(或者串(10),由于多字节字符) 但结果我得到的是:

(or maybe "string(10)" due to multibyte characters) But the result I'm getting is:

Array(1) {
    ["name"]=>
    string(5) "?????"
}

我在想什么,以便统一code字通过AJAX传递?

What am I missing to allow Unicode characters to be passed via AJAX?

编辑:我已经添加了一些code给我的原始数据后是什么,它​​似乎像 ????? 是那里的原始数据后,IE浏览器。单向code甚至没有使它到服务器。

I've added some code to show me what the raw post data is, and it seems like the ????? is there in the raw post data, ie. the Unicode isn't even making it to the server.

推荐答案

在你的页面,有JavaScript的(或正在加载JavaScript文件),你的字符集设置为UTF-8?

On your page that has the JavaScript (or that is loading the JavaScript file) are you setting the charset to UTF-8?

header('Content-Type:text/html; charset=UTF-8');

尝试寻找在Firebug的脚本,并确保该行的字符...

Try looking at your script in Firebug, and make sure the characters in this line...

AJAX("loopback.php",{name:'ヴィックサ'},alert); 

在源文件看起来是一样的,因为他们做的调试。

look the same in your source file as they do in the debugger.

(至少,我想你的脚本,它工作得很好,我只要我设置的字符编码​​。)

( at least, I tried your script and it worked fine for me as soon as I set the character encoding. )

这篇关于AJAX请求返回UNI code字符为问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆