Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JSON数据的第1行第2列中,我一直遇到这个错误SyntaxError:JSON.parse:unexpected字符_Javascript_Jquery_Html_Css_Json - Fatal编程技术网

Javascript 在JSON数据的第1行第2列中,我一直遇到这个错误SyntaxError:JSON.parse:unexpected字符

Javascript 在JSON数据的第1行第2列中,我一直遇到这个错误SyntaxError:JSON.parse:unexpected字符,javascript,jquery,html,css,json,Javascript,Jquery,Html,Css,Json,这一次让我恶心,因为我找不到哪里出了问题。我会的 感谢您的任何帮助或提示。下面是我的javascript代码。到目前为止,服务器端还可以,但问题是在客户端显示实际注释。请帮帮我 $(文档).ready(函数(){ //处理表格 $('form.comments\u form')。每个(函数(){ var form_to_submit=$(此项); 表单提交(功能(事件){ event.preventDefault(); var posta_id=form_to_submit.find(“输入[t

这一次让我恶心,因为我找不到哪里出了问题。我会的 感谢您的任何帮助或提示。下面是我的javascript代码。到目前为止,服务器端还可以,但问题是在客户端显示实际注释。请帮帮我

$(文档).ready(函数(){
//处理表格
$('form.comments\u form')。每个(函数(){
var form_to_submit=$(此项);
表单提交(功能(事件){
event.preventDefault();
var posta_id=form_to_submit.find(“输入[type=hidden].UNIQUE_id”).val();
var tetxarea1=form_to_submit.find(“textarea.target”).val();
$.ajax({
type:'POST',//定义要使用的HTTP谓词的类型(表单的POST)
网址:'http://localhost/Forepost/php/real_time_comment.php“,//我们要发布的url
数据:{
邮递id:posta_id,
tetxarea1:tetxarea1
},//我们的数据对象
dataType:'json',//我们希望从服务器返回什么类型的数据
成功:(功能(响应){
显示注释(jQuery.parseJSON(response));
控制台日志(响应);
}),
错误:函数(){
警惕(“哎呀,出了点问题”);
//哎呀,出了点问题
}
});
//函数显示来自数据库的注释
函数显示注释(响应){
var注释_字符串=”;
注释字符串+=“
  • ”; 注释字符串+=“'”+response.f_name+“'”; comment_string+=“'”+esponse.my_comment+“'”; 注释字符串+=“”; 注释字符串+=“'”+响应。我的注释日期+“'”; //注释字符串+=“回复”; 注释字符串+=“
  • ”; $(“ul.comenting2”).prepend(注释字符串); } //函数显示来自数据库的注释 }); }); });
    我正在尝试使用类“comenting2”将列表显示到无序lis

    更改您的代码
    $(“ul.comenting2”).prepend(注释字符串)

    $(“ul.comenting2”).prepend($(注释字符串))

    是。使问题更容易理解。我相信,如果您还提供JSON响应的示例,那就太好了。这通常是由错误的JSON格式引起的

    好的。得到了您的示例JSON响应。 尝试将代码更改为:

    $.ajax({
                type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
                url: 'http://localhost/Forepost/php/real_time_comment.php', // the url where we want to POST
                data: {
                    posta_id: posta_id,
                    tetxarea1: tetxarea1
                }, // our data object
                dataType: 'json', // what type of data do we expect back from the server
                success: (function (response) {
                    display_the_comment(eval('('+response+')'));
                    console.log(response);
                }),
                error: function () {
                    alert("oops something went wrong");
                    // oops something went wrong
                }
            });
    
            //FUNCTION TO DISPLAY COMMENT FROM DATABASE
            function display_the_comment(response) {
                var comment_string = " ";
                comment_string += "<li class='indiv_cmnts'>";
                comment_string += "<span class='user_fname2'>&lsquo;" + response.f_name + "&rsquo;</span>";
                comment_string += "<div class='my_msg'>&lsquo;" + esponse.my_comment + "&rsquo;</div>";
                comment_string += "<img class='user_proff' src='" + response.profile_img + "'/>";
                comment_string += "<span class='time_cmnts'>&lsquo;" + response.my_comment_date + "&rsquo;</span>";
                //comment_string += "<span class='fa_reply'><i class='fa fa-reply' aria-hidden='true'></i> reply</span>";
                comment_string += "</li>";
    
                $("ul.comenting2").prepend(comment_string);
            }
    
    $.ajax({
    type:'POST',//定义要使用的HTTP谓词的类型(表单的POST)
    网址:'http://localhost/Forepost/php/real_time_comment.php“,//我们要发布的url
    数据:{
    邮递id:posta_id,
    tetxarea1:tetxarea1
    },//我们的数据对象
    dataType:'json',//我们希望从服务器返回什么类型的数据
    成功:(功能(响应){
    显示注释(eval(“(“+response+”)”);
    控制台日志(响应);
    }),
    错误:函数(){
    警惕(“哎呀,出了点问题”);
    //哎呀,出了点问题
    }
    });
    //函数显示来自数据库的注释
    函数显示注释(响应){
    var注释_字符串=”;
    注释字符串+=“
  • ”; 注释字符串+=“&lsquo;+response.f_name+”; comment_string+=“&lsquo;”+response.my_comment+”; 注释字符串+=“”; 注释字符串+=“&lsquo;”+响应。我的注释日期+”; //注释字符串+=“回复”; 注释字符串+=“
  • ”; $(“ul.comenting2”).prepend(注释字符串); }

    请注意ajax success和function display的更改注释(response)

    您的响应值已经是一个对象,因为jquery会自动解析它(您将json作为数据类型)。您正试图用json解析一个对象,当然,该对象包含非法字符。

    您要返回的json数据是什么?我最近遇到了这个问题,这是因为JSON响应中发送了非法字符。请尝试获取用于解析的JSON响应字符串或数据,好吗?这将有助于找出问题所在。我的意思是,我们需要使用jQuery.parsejson解析的“response”数据,您不需要使用JSON.parse,因为您已经告诉jQuery您希望返回JSON数据,它会自动解析,并且您会得到一个response对象。运行console.log(response)而不事先进行解析,以了解我的意思。$my_comment=$rowR['comments'];$my_comment_date=$rowR['date_time_comment'];$time=date(“d-M-Y”,strottime($my_comment_date));$std=新的stdClass();$std->f_name=$f_name;$std->l_name=$l_name;$std->my_comment=$my_comment;$std->my_comment_date=$my_comment_date;$std->profile\u img='';echo json_编码($std);好的,这是我从我的phpObject{f_name:“said”,l_name:“jillo”,我的评论:“rrtr”,我的评论日期:“2017-03-10 10:30:29.000000”,profile_img:“…”}中准备的数据。我得到了这个回复