【问题标题】:uncaught syntaxerror unexpected identifier at jquery ajax urljquery ajax url处未捕获的语法错误意外标识符
【发布时间】:2017-06-28 19:16:37
【问题描述】:

我有这样的 ajax 代码

$.ajax({
            url: "AJAX_POST_URL",
            type: "POST",
            data: {commentId:commentId,commentText:commentText},
            dataType : 'json'
            success: function(data)
            {
                if(data.error=='false'){
                    $('comment'+commentId).html(commentText);
                }
            },
});

我在 url 行遇到此错误“未捕获的语法错误意外标识符”。

请帮忙

提前致谢

【问题讨论】:

  • 尝试使用绝对路径而不是相对路径

标签: jquery ajax


【解决方案1】:

我刚刚指出了您在注释行中所做的语法错误

试试,

$.ajax({
    url: "AJAX_POST_URL",
    type: "POST",
    data: {
        commentId: commentId,
        commentText: commentText
    },
    dataType: 'json',  //Missed a comma over here
    success: function (data) {
        if (data.error == 'false') {
            $('comment' + commentId).html(commentText);
        }
    }  //placed an unncessary comma over here
});

【讨论】:

  • @Srnu 很高兴为您提供帮助..!还要注意这一行的$('comment' + commentId)..无论您是要在评论前使用#还是...!
【解决方案2】:

在评论选择器之前使用 '#'。同样在成功前添加',',成功后删除不必要的','

$.ajax({
      url: "AJAX_POST_URL",
      type: "POST",
      data: {commentId:commentId,commentText:commentText},
      dataType : 'json',
      success: function(data)
      {
       if(data.error=='false'){
          $('#comment'+commentId).html(commentText);
       }
     }
 });

【讨论】:

  • 感谢 RGS 的回复。
【解决方案3】:

试试这个

$.ajax({
        url: 'AJAX_POST_URL',
        type: 'POST',
        data: {commentId:commentId,commentText:commentText},
        dataType : 'json'
        }).success(function(data)
        {
            if(data.error=='false'){
                $('comment'+commentId).html(commentText);
            }
        }).error(function () {
              alert("Something went wrong");
          });

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 2013-11-28
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多