“parsererror“ SyntaxError: Unexpected end of JSON input 报错解决

问题

使用$.ajax调请求,接口200,但没有走success回调函数,反而进入了error报错。
原代码如下:

$('button[type=submit]').click(function () {

    if (!flag) return;

    let params = {
      mobile: mobile,
      extra: {
        landing_page: location.href,
      },
    };
    $.ajax({
      dataType: 'json',
      type: 'post',
      url: '/api/customer/add_sem',
      data: params,
      success: function (result) {
        console.log(result);
        toToast('联系方式提交成功')
      },
      error: function (xhr, textStatus, errorThrown) {
        console.log(xhr, textStatus, errorThrown);
        toToast('提交失败,请稍后重试~')
      }
    });
  })

打印error里的信息后,进行报错查询。发现是因为接口返回的json文件为空,(因为该接口是一个post新增接口,无需返数据到前端,所以后端没有进行设置)

解决

去掉dataType: 'json’选项。

$('button[type=submit]').click(function () {

    if (!flag) return;

    let params = {
      mobile: mobile,
      extra: {
        landing_page: location.href,
      },
    };
    $.ajax({
      type: 'post',
      url: '/api/customer/add_sem',
      data: params,
      success: function (result) {
        console.log(result);
        toToast('联系方式提交成功')
      },
      error: function (xhr, textStatus, errorThrown) {
        console.log(xhr, textStatus, errorThrown);
        toToast('提交失败,请稍后重试~')
      }
    });
  })

提交,OK!成功进入success回调。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值