【问题标题】:Parse error in response of AJAX call在响应 AJAX 调用时解析错误
【发布时间】:2017-07-07 00:02:24
【问题描述】:

我是 javascript 和 jquery 的新手,在我的代码中我发起了一个 AJAX 调用,我得到了以下响应。

我正在尝试实现自动完成功能。我正在使用下面的代码进行 AJAX 调用。

$( "#city" ).autocomplete({
      source: function( request, response ) {
        jQuery.ajax({
          url: "the url",
          data: {SearchTerm: request.term}
          success: function (data) {
               console.log("the data is" +data); 
               response(data);
            }
        }).fail(function (jqXHR, textStatus, error) {
             console.log("failure1" + textStatus);
             console.log("failure2" + jqXHR.status);
    });
      },
      minLength: 3,
      select: function( event, ui ) {
        console.log( ui.item ?
          "Selected: " + ui.item.label :
          "Nothing selected, input was " + this.value);
      },
      open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
      },
      close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
      }
    });
  });

即使我看到来自服务器的 200 响应和 JSON 格式的响应,也不会调用成功方法并调用失败方法。由于我收到状态为 200 的 JSON 响应,不应该调用成功方法吗?

【问题讨论】:

  • 这是无效的 JSON。您的服务器端组件(ViewCustomers?)正在转义所有引号。
  • 谢谢詹姆斯。抱歉,你能告诉我这个 JSON 响应应该是什么样子吗?
  • {"suggestions": ["name1", "name2", "name3"]}
  • 我有另一个疑问,当我将 json 从服务器发送到客户端时,服务器会自动添加转义字符。有没有办法防止这种情况?我以 {"suggestions": ["name1", "name2", "name3"]} 格式发送响应
  • 修复您的服务器代码以生成有效的 JSON。就是这样。

标签: javascript jquery json ajax


【解决方案1】:

试试这个,可能对你有帮助:

选项 1:

可能是你不小心在 PHP 中打开了 Magic Quotes
但是这个函数在 PHP 5.3.0 中已经被弃用了。
并从 PHP 5.4.0 开始删除。如here

中所述
  • 转到您的 php.ini 文件
  • 搜索这个关键字:magic_quotes_gpc
  • 将其设置为关闭:magic_quotes_gpc = Off

选项 2:

像这样在你的代码上做:

if (get_magic_quotes_gpc()) {
    $returnedValue = stripslashes($yourReturnedValue);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2014-07-07
    • 2017-08-05
    • 1970-01-01
    相关资源
    最近更新 更多