json_en code不工作与HTML字符串值字符串值、工作、json_en、code

2023-09-10 16:39:44 作者:爱我如何

我调试这个AJAX相当一段时间了。我有这个在我的jQuery的文件:

  $(#typeForm)。当作ajaxForm({
    成功:函数(HTML){
        警报(HTML);
})。提交();
 

这要求 service.php ,并在其中我有这样的:

  $数据=阵列('upload_data'=> $这个 - > upload->数据());
$海峡=<股利风格='位置:相对'>< IMG SRC =/资产/ UI / success.png/><跨度风格='位置:相对;顶端:-15px; > Nachricht empfangen<!/ SPAN>< / DIV>中;
回声json_en code(阵列('FILE_NAME'=> $数据['upload_data'] ['FILE_NAME'],'提示'=> $ STR));
 

这是行不通的。但是,通过替换 $ STR $海峡=HELLO WORLD; jQuery的提醒我应该怎么期望。似乎是什么问题?

编辑:

php json encode 中文不转码怎么办

下面是输出的的screenie:

它确实警报,但如果我修改我的jQuery到这一点:

  $(#typeForm)。当作ajaxForm({
    成功:函数(HTML){
        VAR OBJ = $ .parseJSON(HTML);
        警报(OBJ);
})。提交();
 

然后什​​么事情都不做,甚至报警。

我做的 json_en code A的var_dump,这里是转储,它看起来像一个畸形的JSON:

 字符串(214){FILE_NAME:cde595988d386529909ce5a8fe3a6d6f.png,提示:< D​​IV的风格=的位置是:相对;>< IMG SRC =\ /资产\ / UI \ /success.png\ =><跨度风格=的位置是:相对;顶端:-15px;> Nachricht empfangen和放大器; LT; \ / SPAN&放大器; GT ;&放大器; LT; \ / DIV&放大器; GT;}
< / SPAN>< / DIV>
 

下面是完整内容的 service.php

 类服务延伸是CI_Controller
{
    公共职能__construct()
    {
        父:: __结构();
    }
    公共功能指数()
    {
        $文件名='uploadfile';

        $配置['upload_path'] ='./uploads/temp';
        $配置['allowed_types'] ='JPG | PNG | GIF | DOC | DOCX | PDF | PPT | PPTX | XLS | XLSX | BMP';
        $配置['MAX_SIZE'] ='3072';
        $配置['encrypt_name'] = TRUE;
        $配置['remove_spaces'] = TRUE;

        $这个 - >负载>库('上传',$配置);

        如果($这个 - >!upload-> do_upload($文件名))
        {
            $错误=阵列('错误'=> $这个 - > upload->的display_errors());
                    回声json_en code(阵列('错误'=> $错误['错误']));
        }
        其他
        {
            $数据=阵列('upload_data'=> $这个 - > upload->数据());
            $ FILE_NAME = $数据['upload_data'] ['FILE_NAME'];
            //的print_r($的数据);
            //回声json_en code(阵列('测试'=>的Hello World));
            $海峡=<股利风格='位置:相对;>< IMG SRC =/资产/ UI / success.png/><跨度风格='位置:相对;顶端:-15px; '> Nachricht empfangen<!/ SPAN>< / DIV>中;
            $ str2的= json_en code(阵列(FILE_NAME=> $ FILE_NAME,提示=> $ STR));
            //后续代码var_dump($ STR2);
            退出(json_en code(阵列('FILE_NAME'=> $数据['upload_data'] ['FILE_NAME'],'提示'=> $ STR)));
        }
    }
}
 

解决方案

我有同样的问题与 json_en code 今天。但测试了很多之后,我找到了正确的解决方案:

在PHP中EN code数组或字符串:

  PHP:json_en code(ARRAY,JSON_HEX_QUOT | JSON_HEX_TAG);
 

在JavaScript中去code相同的:

  JS:变种D = $ .parseJSON(内容);
 

I am debugging this ajax for quite a time now. I have this on my jQUery file:

$("#typeForm").ajaxForm({
    success : function(html){
        alert(html);
}).submit();

This calls service.php, and within it I have this:

$data = array('upload_data' => $this->upload->data());
$str = "<div style='position:relative'><img src='/assets/ui/success.png' /><span style='position:relative;top:-15px;'>Nachricht empfangen!</span></div>";
echo json_encode(array('file_name' => $data['upload_data']['file_name'], 'prompt' => $str));

This won't work. But by replacing $str to $str = "HELLO WORLD"; the jQuery alerts what should I expected. What seems to be the problem?

EDIT:

Here is a screenie of the output:

It does alerts, but if I modify my jQuery into this:

$("#typeForm").ajaxForm({
    success : function(html){
        var obj = $.parseJSON(html);
        alert(obj);
}).submit();

Then it does nothing at all, even alerting.

I did a var_dump on the json_encode and here is the dump, it looks like a malformed JSON:

string(214) "{"file_name":"cde595988d386529909ce5a8fe3a6d6f.png","prompt":"<div style="position:relative;"><img src="\/assets\/ui\/success.png" \=""><span style="position:relative;top:-15px;">Nachricht empfangen!&lt;\/span&gt;&lt;\/div&gt;"}"
</span></div>

Here is the full content of service.php

class Service extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $filename = 'uploadfile';

        $config['upload_path'] = './uploads/temp';
        $config['allowed_types'] = 'jpg|png|gif|doc|docx|pdf|ppt|pptx|xls|xlsx|bmp';
        $config['max_size'] = '3072';
        $config['encrypt_name'] = TRUE;
        $config['remove_spaces'] = TRUE;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($filename))
        {
            $error = array('error' => $this->upload->display_errors());
                    echo json_encode(array('error' => $error['error']));
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $file_name = $data['upload_data']['file_name'];
            //print_r($data);
            //echo json_encode(array('test' => "Hello World"));
            $str = "<div style='position:relative;'><img src='/assets/ui/success.png' /><span style='position:relative;top:-15px;'>Nachricht empfangen!</span></div>";
            $str2 = json_encode(array("file_name" => $file_name, "prompt" => $str));
            //var_dump($str2);
            exit(json_encode(array('file_name' => $data['upload_data']['file_name'], 'prompt' => $str)));
        }
    }
}

解决方案

I was having same problem with json_encode today. But after testing a lot I found the correct solution :

In php to encode the array or string:

PHP: json_encode(ARRAY, JSON_HEX_QUOT | JSON_HEX_TAG);

In javascript to decode the same:

JS: var d = $.parseJSON(content);