将编码的php json传递给javascript

时间:2015-02-03 11:48:24

标签: javascript php ajax json backbone.js

我的应用程序在PHP和javascript中出现问题。

我对一个从数据库中获取值的函数php进行了ajax调用。 我检索了许多带字段的记录,在其中我有一个名为description的字段,其值如下:

<p><strong>TEST IT</strong></p><p>non gras<span style="color: rgb(192, 145, 0);">sett</span>o  </p>

我对json进行编码后,在javascript中获取并打印出来。但结果是:

TEST+IT

non+grassetto++

我的代码是这样的:

AJAX电话:

$.ajax({   
                url: site_url_database, 
                type: "GET", 
                async: true,
                data: window.location.search, 
                dataType: "json",
                timeout: 30000,
                success: function(data) {
                    _.each(data.hotel, function(d, index_d) {
                        data.hotel[index_d].description = decodeURIComponent(d.description);
                    });
                },
                error: function(data) {

                }
            })

获取字段描述的PHP函数:

....
$hotel_array['description'] = urlencode($row->hotel_description);
....
//encode array to return
$hotel_array = json_encode($hotel_array);
echo($hotel_array);

我在javascript(Backbone)中打印后,如果我创建了console.log(),我就明白了:

TEST+IT

    non+grassetto++

如何打印好?

由于

P.S。我无法改变工作方法所以我需要一个ajax调用一个函数php并在Backbone / javascript中打印它我只需要编码和解码好

我已经在php中试过了:

$hotel_array['description'] = addslashes(htmlentities($row->hotel_description));

在javascript中我尝试过使用: https://raw.githubusercontent.com/kvz/phpjs/master/functions/url/urldecode.js

HTML Entity Decode

2 个答案:

答案 0 :(得分:1)

Javascript&#39; decodeURIComponent()不会删除PHP +添加的urlencode()字符;它希望空间编码为%20,而不是+

$hotel_array['description'] = urlencode($row->hotel_description);

php > echo $hotel_array['description'];
%3Cp%3E%3Cstrong%3ETEST+IT%3C%2Fstrong%3E%3C%2Fp%3E%3Cp%3Enon+gras%3Cspan+style%3D%22color%3A+rgb%28192%2C+145%2C+0%29%3B%22%3Esett%3C%2Fspan%3Eo++%3C%2Fp%3E

然后,在Javascript中:

>js s = '%3Cp%3E%3Cstrong%3ETEST+IT%3C%2Fstrong%3E%3C%2Fp%3E%3Cp%3Enon+gras%3Cspan+style%3D%22color%3A+rgb%28192%2C+145%2C+0%29%3B%22%3Esett%3C%2Fspan%3Eo++%3C%2Fp%3E';
>js decodeURIComponent(s);
<p><strong>TEST+IT</strong></p><p>non+gras<span+style="color:+rgb(192,+145,+0);">sett</span>o++</p>

如您所见,+字符仍保留在已解码的字符串中。

相反,您可以使用遵循RFC 3986的rawurlencode()并将空格编码为%20

php > $s = rawurlencode('<p><strong>TEST IT</strong></p><p>non gras<span style="color: rgb(192, 145, 0);">sett</span>o  </p>');
php > echo $s;
%3Cp%3E%3Cstrong%3ETEST%20IT%3C%2Fstrong%3E%3C%2Fp%3E%3Cp%3Enon%20gras%3Cspan%20style%3D%22color%3A%20rgb%28192%2C%20145%2C%200%29%3B%22%3Esett%3C%2Fspan%3Eo%20%20%3C%2Fp%3E

然后在Javascript中:

js> decodeURIComponent('%3Cp%3E%3Cstrong%3ETEST%20IT%3C%2Fstrong%3E%3C%2Fp%3E%3Cp%3Enon%20gras%3Cspan%20style%3D%22color%3A%20rgb%28192%2C%20145%2C%200%29%3B%22%3Esett%3C%2Fspan%3Eo%20%20%3C%2Fp%3E');
<p><strong>TEST IT</strong></p><p>non gras<span style="color: rgb(192, 145, 0);">sett</span>o  </p>

或者,如果您无法修改PHP代码,则可以使用+手动替换' '

js> s = '%3Cp%3E%3Cstrong%3ETEST+IT%3C%2Fstrong%3E%3C%2Fp%3E%3Cp%3Enon+gras%3Cspan+style%3D%22color%3A+rgb%28192%2C+145%2C+0%29%3B%22%3Esett%3C%2Fspan%3Eo++%3C%2Fp%3E';
js> decodeURIComponent(s).replace(/\+/g, ' ');
<p><strong>TEST IT</strong></p><p>non gras<span style="color: rgb(192, 145, 0);">sett</span>o  </p>

答案 1 :(得分:0)

似乎PHP在将HTML标记传递给浏览器之前将其剥离。在编码之前,请检查以确保PHP函数中没有strip_tags()函数。