【问题标题】:I keep getting this error SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data我不断收到此错误 SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
【发布时间】:2017-03-10 09:03:35
【问题描述】:

这个让我恶心。因为我找不到哪里出错了。我会的 感谢您的任何帮助或提示。下面是我的javascript代码。到目前为止,服务器端很好,但在客户端显示实际评论是问题所在。请帮帮我。

$(document).ready(function () {
    // process the form
    $('form.comments_form').each(function () {
        var form_to_submit = $(this);
        form_to_submit.submit(function (event) {
            event.preventDefault();
            var posta_id = form_to_submit.find("input[type=hidden].UNIQUE_ID").val();
            var tetxarea1 = form_to_submit.find("textarea.target").val();

            $.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(jQuery.parseJSON(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'>'" + response.f_name + "'</span>";
                comment_string += "<div class='my_msg'>'" + esponse.my_comment + "'</div>";
                comment_string += "<img class='user_proff' src='" + response.profile_img + "'/>";
                comment_string += "<span class='time_cmnts'>'" + response.my_comment_date + "'</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);
            }
            //FUNCTION TO DISPLAY COMMENT FROM DATABASE
        });
    });
});

我正在尝试将列表显示为具有“commenting2”类的无序列表

【问题讨论】:

  • 您返回的 JSON 数据是什么?我最近遇到了这个问题,这是由于在 JSON 响应中发送了非法字符。
  • 您能否尝试获取您用于解析的 JSON 响应字符串或数据?这将有助于找出问题。我的意思是我们需要使用 jQuery.parseJSON 解析的“响应”数据
  • 你不需要使用 JSON.parse,因为你已经告诉 jquery 你期望 json 数据返回它会被自动解析并且你得到一个响应对象。运行 console.log(response) 而不事先解析,看看我的意思。
  • $my_comment = $rowR['cmets']; $my_comment_date = $rowR['date_time_commented']; $time = date("d-M-Y", strtotime($my_comment_date)); $std=新标准类(); $std->f_name=$f_name; $std->l_name=$l_name; $std->my_comment=$my_comment; $std->my_comment_date=$my_comment_date; $std->profile_img='localhost/Forepost/icons/forepost_user.png'; echo json_encode($std);
  • 好的,这是我从我的 php 准备的数据

标签: javascript jquery html css json


【解决方案1】:

更改您的代码 $("ul.comenting2").prepend(comment_string);
$("ul.comenting2").prepend($(comment_string));

【讨论】:

  • 对象 { f_name: "said", l_name: "jillo", my_comment: "rrtr", my_comment_date: "2017-03-10 10:30:29.000000", profile_img: "localhost/Forepost/icons/for... " }
  • 我收到了回复
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    是的。为了更容易理解问题。我相信如果您还提供了 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成功和函数display_the_comment(response)的变化

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 2016-05-16
      • 2018-02-03
      • 1970-01-01
      • 2019-01-31
      相关资源
      最近更新 更多