欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

php json encode html,php – json_encode不能使用html字符串作为值

发布时间:2025/5/22 php 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php json encode html,php – json_encode不能使用html字符串作为值 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

我现在调试这个ajax已经有一段时间了。我在我的jQUery文件中有这个:

$("#typeForm").ajaxForm({

success : function(html){

alert(html);

}).submit();

这叫做service.php,在其中我有这个:

$data = array('upload_data' => $this->upload->data());

$str = "

Nachricht empfangen!";

echo json_encode(array('file_name' => $data['upload_data']['file_name'], 'prompt' => $str));

这不行。但是将$ str替换为$ str =“HELLO WORLD”; jQuery提醒我应该预期的。什么似乎是问题?

编辑:

这是一个输出的屏幕:

它发出警报,但是如果我将jQuery修改为:

$("#typeForm").ajaxForm({

success : function(html){

var obj = $.parseJSON(html);

alert(obj);

}).submit();

那么它什么都不做,甚至警报。

我在json_encode上做了一个var_dump,这里是转储,它看起来像一个格式错误的JSON:

string(214) "{"file_name":"cde595988d386529909ce5a8fe3a6d6f.png","prompt":"

Nachricht empfangen!<\/span><\/div>"}"

这是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 = "

Nachricht empfangen!";

$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)));

}

}

}

总结

以上是生活随笔为你收集整理的php json encode html,php – json_encode不能使用html字符串作为值的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。