簡體   English   中英

SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> )

[英]SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)

我已將reCaptcha添加到我的Firebase項目中。 使用發送按鈕,我正在使用grecaptcha.getResponse()向我的服務器發送表單數據以及來自驗證碼的響應。

這是client.js代碼:

 $('.sendUrl').on('click', function(e){ e.preventDefault() ; $('#nonauthForm').validate({ rules: { url_nauth: { required: true, url: true } } }) ; if( $('#nonauthForm').valid()){ var captcha_val = grecaptcha.getResponse(widget1) ; $.post('/nonauth_url',{ URL: $('#url').val(), CAPTCHA: captcha_val },function(data, status){ if(status === 'success'){ if(data.val === 1){ $('#newurl').html(`${window.location.origin+ '/'+ data.code}`) ; $('#url').val('') ; grecaptcha.reset(widget1) ; } else if( data.val === 0 ){ alert('Please select the captcha to continue !!') ; } else if( data.val === -1){ alert('Failed to verify captcha !!') ; grecaptcha.reset(widget1) ; } } }).fail(function(res){ errorAlert('Error occurred', 'An error happend while connecting to the server !') ; }) ; } else{ $('label.error').addClass('text-danger d-block mt-2'); } }) ; 

這是我的節點服務器代碼(index.js):

 const express = require('express') ; const urlId = require('shortid') ; const string = require('randomstring') ; const app = express() ; require('dotenv').config() ; const secretkey = process.env.SECRETKEY ; app.post('/nonauth_url', (req, res) => { let captchaRes = req.body.CAPTCHA ; if(captchaRes === undefined || captchaRes === '' || captchaRes === null) { return res.send({ val: 0 , code: null }) ; } let verification_url = `https://www.google.com/recaptcha/api/siteverify?secret=${secretkey}&response=${captchaRes}&remoteip=${req.connection.remoteAddress}` ; request(verification_url, function(err,response,body){ let data = JSON.parse(body) ; if( data.success !== undefined && !data.success){ return res.send({ val: -1, code: null }) ; } let codeGenerated = 'n' + urlId.generate() ; // n for non-authenticated users let docname = 'doc-' + string.generate(4) ; let schema = { code: codeGenerated, url: req.body.URL, expiredAt: Date.now()+600000 // in milisceonds for 10min } let docRef = db.collection('nonauth_url').doc(docname).set(schema).then(() => { res.send({ val: 1, code: schema.code }) ; }).catch(err => { console.log(err) ; }) ; }) ; }) ; exports.routeFunc = functions.https.onRequest(app) ; 

因此,如果用戶不檢查驗證碼,則驗證碼將返回代碼0,成功返回代碼1和失敗返回代碼-1。 這段代碼可以在localhost上正常運行。 但是在我的托管網址**。firebaseapp.com上,如果我沒有檢查驗證碼,它會回饋對驗證碼的要求。 但是,當我檢查Recaptcha並將請求發送到服務器時,它給出的錯誤代碼為500。

這是主項目控制台中用於我功能的控制台: 在此處輸入圖片說明

編輯:現在我已經使用axios而不是請求

新代碼:

 axios.get(verification_url).then( res => { return res.data ; }).then( object => { if(object.success){ let codeGenerated = 'n' + urlId.generate() ; // n for non-authenticated users let docname = 'doc-' + string.generate(4) ; let schema = { code: codeGenerated, url: req.body.URL, expiredAt: Date.now()+600000 // in milisceonds for 10min } let docRef = db.collection('nonauth_url').doc(docname).set(schema).then(() => { res.send({ val: 1, code: schema.code }) ; }).catch(err => { console.log(err) ; }) ; } else{ res.send({ val: -1, code: null }) ; } }).catch(err => { console.log("Error occurred in axios",err) ; }) ; }) ; 

現在,出現以下錯誤: Error occurred in axios

{ Error: getaddrinfo EAI_AGAIN www.google.com:443 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) errno: 'EAI_AGAIN', code: 'EAI_AGAIN', syscall: 'getaddrinfo', hostname: 'www.google.com', host: 'www.google.com', port: 443 . . . . }

因此,我能夠通過更改驗證網址(即www.google.com/recaptcha/api/siteverify來解決此問題,這不適用於僅針對node.js的firebase項目,有關在此處檢查reCaptcha for firebase 鏈接的問題


同時將我以前的答案更改為:

 const rp = require('request-promise') ; rp({ uri: 'https://recaptcha.google.com/recaptcha/api/siteverify', method: 'POST', formData: { secret: secretkey, response: captchaRes }, json: true }).then(result => { console.log("recaptcha result", result) if (result.success) { // for human detection } else { // for bot detection } }).catch(reason => { // error handling }) ; 

並將其部署在Firebase上終於可以使用了。 我認為firebase不允許致電我需要付費計划的較舊的Recaptcha驗證地址,但在使用Recaptcha搜索Firebase時解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM