Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

javascript - 未捕获(承诺)SyntaxError:JSON输入意外结束(Uncaught (in promise) SyntaxError: Unexpected end of JSON input)

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.(我正在尝试向服务器发送新的推送订阅,但是遇到错误“未捕获(承诺)SyntaxError:JSON输入意外结束”,控制台表示它在我的索引页的第1行,显然不是这种情况。)

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:(我怀疑发生问题的函数(因为在我注释掉该错误时未抛出错误)是sendSubscriptionToBackEnd(subscription) ,该函数在以下调用:) 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.'); } }); } I have tried replacing single quotes with double quotes in the fetch call but that yields the same results.(我尝试在fetch调用中将单引号替换为双引号,但结果相同。) I know that the JSON should be populated because it prints to the screen in the updateSubscriptionOnServer() function with subscriptionJson.textContent = JSON.stringify(subscription);(我知道应该填充JSON,因为它会使用subscriptionJson.textContent = JSON.stringify(subscription);来打印到updateSubscriptionOnServer()函数中的屏幕subscriptionJson.textContent = JSON.stringify(subscription);) , and I used that output in the google codelab's example server to receive a push successfully.(,并且我使用了Google Codelab示例服务器中的输出来成功接收到推送。) EDIT: Here is the JSON as a string, but I don't see a mistake in syntax:(编辑:这是作为字符串的JSON,但我没有看到语法错误:) {"endpoint":"https://fcm.googleapis.com/fcm/send/dLmthm1wZuc:APA91bGULRezL7SzZKywF2wiS50hXNaLqjJxJ869y8wiWLA3Y_1pHqTI458VIhJZkyOsRMO2xBS77erpmKUp-Tg0sMkYHkuUJCI8wEid1jMESeO2ExjNhNC9OS1DQT2j05BaRgckFbCN","keys":{"p256dh":"BBz2c7S5uiKR-SE2fYJrjPaxuAiFiLogxsJbl8S1A_fQrOEH4_LQjp8qocIxOFEicpcf4PHZksAtA8zKJG9pMzs=","auth":"VOHh5P-1ZTupRXTMs4VhlQ=="}} Any ideas??(有任何想法吗??)   ask by zoltar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This might be a problem with the endpoint not passing the appropriate parameters in the response's header.(这可能是端点未在响应的标头中传递适当的参数的问题。)

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(在Chrome浏览器的控制台的“网络”标签内,检查端点发送的标头,并且标头应包含以下内容: 正确响应示例,以允许来自本地主机的请求和跨域请求) Ask the API developer to include this in the headers:(要求API开发人员在标头中添加以下内容:) "Access-Control-Allow-Origin" : "*", "Access-Control-Allow-Credentials" : true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...