Javascript:意外的令牌非法

发布于 2024-11-28 19:51:16 字数 1114 浏览 0 评论 0原文

我在 Chrome 中遇到错误 Uncaught SyntaxError: Unexpected token ILLEGAL

该代码存在

$("form#new_redemption").live('submit', function() {
  event.preventDefault();
  var that    = $(this);

  var action  = that.attr('action');
  var data    = that.serialize();

  $.ajax({
    type: "POST",
    url:  action,
    data: data,
    dataType: 'json',
    beforeSend: function(request) {
      request.setRequestHeader("Accept", "application/json");
    },
    success: function(res) {
      var response = JSON.parse(res.responseText); // <~~~ Unexpected token ILLEGAL
      if (response.message) {
        that.slideUp();
        $("#results").html(response.message).attr('class', 'notice').slideDown();
      }
      else if (response.url) {
        window.location = response.url 
      }
    },
    error: function(res) {
      var response = JSON.parse(res.responseText);
      $('#results').html(response.error).attr('class', 'error').slideDown();
    }
  });
  return false;
});

错误,该代码效果很好。但每次成功响应时我都会收到错误。这里有问题吗? VIM 有没有办法突出显示代码中非法的 javascript 字符?

谢谢你!

I have the error Uncaught SyntaxError: Unexpected token ILLEGAL in Chrome.

The code is

$("form#new_redemption").live('submit', function() {
  event.preventDefault();
  var that    = $(this);

  var action  = that.attr('action');
  var data    = that.serialize();

  $.ajax({
    type: "POST",
    url:  action,
    data: data,
    dataType: 'json',
    beforeSend: function(request) {
      request.setRequestHeader("Accept", "application/json");
    },
    success: function(res) {
      var response = JSON.parse(res.responseText); // <~~~ Unexpected token ILLEGAL
      if (response.message) {
        that.slideUp();
        $("#results").html(response.message).attr('class', 'notice').slideDown();
      }
      else if (response.url) {
        window.location = response.url 
      }
    },
    error: function(res) {
      var response = JSON.parse(res.responseText);
      $('#results').html(response.error).attr('class', 'error').slideDown();
    }
  });
  return false;
});

On errors, this code works great. But every time its a successful response I get an error. Is there a problem here? And is there a way in VIM to highlight illegal javascript characters in the code?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荆棘i 2024-12-05 19:51:16

dataType 设置为 json 将在 success 回调中自动解析响应 JSON。

试试这个:

$("form#new_redemption").live('submit', function() {
  event.preventDefault();
  var that    = $(this);

  var action  = that.attr('action');
  var data    = that.serialize();

  $.ajax({
    type: "POST",
    url:  action,
    data: data,
    dataType: 'json',
    beforeSend: function(request) {
      request.setRequestHeader("Accept", "application/json");
    },
    success: function(res) {
      if (response.message) {
        that.slideUp();
        $("#results").html(response.message).attr('class', 'notice').slideDown();
      }
      else if (response.url) {
        window.location = response.url 
      }
    },
    error: function(res) {
      var response = JSON.parse(res.responseText);
      $('#results').html(response.error).attr('class', 'error').slideDown();
    }
  });
  return false;
});

Setting dataType to json will automagically parse the response JSON for you within the success callback.

Try this:

$("form#new_redemption").live('submit', function() {
  event.preventDefault();
  var that    = $(this);

  var action  = that.attr('action');
  var data    = that.serialize();

  $.ajax({
    type: "POST",
    url:  action,
    data: data,
    dataType: 'json',
    beforeSend: function(request) {
      request.setRequestHeader("Accept", "application/json");
    },
    success: function(res) {
      if (response.message) {
        that.slideUp();
        $("#results").html(response.message).attr('class', 'notice').slideDown();
      }
      else if (response.url) {
        window.location = response.url 
      }
    },
    error: function(res) {
      var response = JSON.parse(res.responseText);
      $('#results').html(response.error).attr('class', 'error').slideDown();
    }
  });
  return false;
});
木槿暧夏七纪年 2024-12-05 19:51:16

为了扩展上面的评论之一,我收到此错误是因为返回的 JSON 结果存在问题。具体来说,JSON 响应数据中的字符串值之一包含未转义的双引号。就我而言,我调用的是我自己的 Ajax 函数,因此我修复它的方法是在返回 JSON 数据之前转义服务器上的双引号。然后我发现换行符也有同样的问题,所以我使用了在另一篇文章中找到的 str_replace 调用:
PHP 的 json_encode 不会转义所有 JSON 控制字符

function escapeJsonString($value) {
    # list from www.json.org: (\b backspace, \f formfeed)    
    $escapers =     array("\\",     "/",   "\"",  "\n",  "\r",  "\t", "\x08", "\x0c");
    $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t",  "\\f",  "\\b");
    $result = str_replace($escapers, $replacements, $value);
    return $result;
}

To expand on one of the comments above, I was getting this error because of a problem in the JSON results that were being returned. Specifically, one of the string values in the JSON response data had an unescapted double quote in it. In my case, it was my own Ajax function that I was calling, so what I did to fix it was to escape the double quotes on the server before returning the JSON data. Then I discovered that I had the same problem with newline characters, so I used a str_replace call that I found in another post:
PHP's json_encode does not escape all JSON control characters

function escapeJsonString($value) {
    # list from www.json.org: (\b backspace, \f formfeed)    
    $escapers =     array("\\",     "/",   "\"",  "\n",  "\r",  "\t", "\x08", "\x0c");
    $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t",  "\\f",  "\\b");
    $result = str_replace($escapers, $replacements, $value);
    return $result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文