未捕获(在promise中)SyntaxError:JSON输入的意外结束 [英] Uncaught (in promise) SyntaxError: Unexpected end of JSON input

查看:429
本文介绍了未捕获(在promise中)SyntaxError:JSON输入的意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的服务器发送新的推送订阅,但遇到错误未捕获(在承诺中)SyntaxError:JSON输入的意外结束并且控制台说它在我的索引页面的第1行,显然是不是这样。

I am trying to send a new push subscription to my server but am encountering an error "Uncaught (in promise) SyntaxError: Unexpected end of JSON input" and the console says it's in my index page at line 1, which obviously is not the case.

我怀疑发生问题的函数(因为我发表评论时没有抛出错误)是 sendSubscriptionToBackEnd(订阅) 在以下内容中调用:

The function where I suspect the problem occurring (because error is not thrown when I comment it out) is sendSubscriptionToBackEnd(subscription) which is called in the following:

function updateSubscriptionOnServer(subscription) {
  const subscriptionJson = document.querySelector('.js-subscription-json');
  const subscriptionDetails = document.querySelector('.js-subscription-details');

  if (subscription) {
    subscriptionJson.textContent = JSON.stringify(subscription);
    sendSubscriptionToBackEnd(subscription);
    subscriptionDetails.classList.remove('is-invisible');
  } else {
    subscriptionDetails.classList.add('is-invisible');
  }
}

函数本身(在上述函数之前):

The function itself (which precedes the above function):

 function sendSubscriptionToBackEnd(subscription) {
  return fetch('/path/to/app/savesub.php', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(subscription)
  })
  .then(function(response) {
    if (!response.ok) {
      throw new Error('Bad status code from server.');
    }

    return response.json();
  })
  .then(function(responseData) {
    if (!(responseData.data && responseData.data.success)) {
      throw new Error('Bad response from server.');
    }
  });
}

我试过在fetch调用中用双引号替换单引号但是产生了相同的结果。

I have tried replacing single quotes with double quotes in the fetch call but that yields the same results.

我知道应填充JSON,因为它会在 updateSubscriptionOnServer()函数与 subscriptionJson.textContent = JSON.stringify(subscription); ,我在google codelab的示例服务器中使用该输出成功接收推送。

I know that the JSON should be populated because it prints to the screen in the updateSubscriptionOnServer() function with subscriptionJson.textContent = JSON.stringify(subscription);, and I used that output in the google codelab's example server to receive a push successfully.

编辑:这是JSON作为字符串,但我没有看到语法错误:

Here is the JSON as a string, but I don't see a mistake in syntax:

{"endpoint":"https://fcm.googleapis.com/fcm/send/dLmthm1wZuc:APA91bGULRezL7SzZKywF2wiS50hXNaLqjJxJ869y8wiWLA3Y_1pHqTI458VIhJZkyOsRMO2xBS77erpmKUp-Tg0sMkYHkuUJCI8wEid1jMESeO2ExjNhNC9OS1DQT2j05BaRgckFbCN","keys":{"p256dh":"BBz2c7S5uiKR-SE2fYJrjPaxuAiFiLogxsJbl8S1A_fQrOEH4_LQjp8qocIxOFEicpcf4PHZksAtA8zKJG9pMzs=","auth":"VOHh5P-1ZTupRXTMs4VhlQ=="}}

任何想法??

推荐答案

这可能是端点没有在响应标题中传递适当参数的问题。

This might be a problem with the endpoint not passing the appropriate parameters in the response's header.

在Chrome中控制台,在网络选项卡内,检查端点发送的标头,它应包含:
允许来自localhost和跨域请求的请求的正确响应示例

In Chrome's console, inside the Network tab, check the headers sent by the endpoint and it should contain this: Example of proper response to allow requests from localhost and cross domains requests

请API开发人员将其包含在标题中:

Ask the API developer to include this in the headers:

"Access-Control-Allow-Origin" : "*", 
"Access-Control-Allow-Credentials" : true 

这篇关于未捕获(在promise中)SyntaxError:JSON输入的意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆