在发布json_encode数据时请求的JSON解析失败了,我怎么能复活" \"

时间:2013-05-01 12:57:02

标签: php jquery ajax json

$valueArr=htmlspecialchars(json_encode($event), ENT_QUOTES, 'UTF-8');

我从

发送json_encode($valueArr)
<a href="javascript:void(0)"  id="event_<?=$event->event_id?>"  onclick='getDetailsEvent(<?php echo $valueArr;?>)' >

到这个功能

 <script>
    function getDetailsEvent(eObj){
        var edStr=JSON.stringify(eObj);
        m.ajax({
            type: "POST",
            url: "<?php echo SITEPATH;?>/socialuser/event_calender/gethtml.php",
            data: "ed="+edStr+"&t=<?=time()?>",
            contentType: 'application/json; charset=utf-8',
            dataType: "json",
            error: function(jqXHR, exception){
                if (jqXHR.status === 0){
                    alert('Not connect.\n Verify Network.');
                }else if (jqXHR.status == 404){
                    alert('Requested page not found. [404]');
                }else if (jqXHR.status == 500){
                    alert('Internal Server Error [500].');
                }else if (exception === 'parsererror'){
                    alert('Requested JSON parse failed.');
                }else if (exception === 'timeout'){
                    alert('Time out error.');
                }else if (exception === 'abort'){
                    alert('Ajax request aborted.');
                }else{
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            },
            beforeSend:function(){
                var bSend = 'l o a d i n g . . . ';
                m('#load').show();
                m('#load').html(bSend);
            },
            success: function(response){
                m('#evenDetails').html(response);
            }
        });
    }
</script>

现在,在这个功能上,如果我在本地服务器中没有使用“datatype:”json“”,那么每件事都会有效。 但在服务器上这不起作用..

它会给出错误

else if (exception === 'parsererror'){
                        alert('Requested JSON parse failed.');

关于ajax电话..

在打印帖子数据的帖子页面上,数据如此

{\"first_name\":\"Pradeep\",\"last_name\":\"Kumar\",\"profile_image\":\"thumbnail_10000022_1359466283.jpg\",\"category\":\"Restaurant\",\"category_image\":\"icon_restaurants.png\",\"category_icons\":\"restaurants_icon.png\",\"event_id\":\"17\",\"social_users_id\":\"10000022\",\"location\":\"new, Thandla, Madhya Pradesh 457777, India\",\"lat\":\"23.0048561\",\"lng\":\"74.57584440000005\",\"title\":\"dasdasd\",\"event\":\"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p\",\"pic_event\":\"517a1d4a38d411366957386.jpg\",\"event_type\":\"1\",\"day\":\"15\",\"month\":\"5\",\"year\":\"2013\",\"date\":\"1368556200\",\"time\":\"0\"}

我的问题是,如果我得到这种类型的数据,我怎么能json_decode,因为它包含不会解析服务器上数据的“\”。

1 个答案:

答案 0 :(得分:1)

htmlspecialchars函数添加特殊字符以避免安全漏洞,其中一个字符是'/'字符。我建议只使用json_encode($event)