简体   繁体   English

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

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

I have added reCaptcha to my firebase project. 我已将reCaptcha添加到我的Firebase项目中。 Using send button I am sending form data as well as the response from captcha using grecaptcha.getResponse() to my server. 使用发送按钮,我正在使用grecaptcha.getResponse()向我的服务器发送表单数据以及来自验证码的响应。

here is client.js code: 这是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'); } }) ; 

here is my node server code (index.js): 这是我的节点服务器代码(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) ; 

So, with this if user doesn't check the captcha its will return code 0, code 1 for success & code -1 for failure in verification. 因此,如果用户不检查验证码,则验证码将返回代码0,成功返回代码1和失败返回代码-1。 This code is working perfectly on localhost. 这段代码可以在localhost上正常运行。 But on my hosting url **.firebaseapp.com if I'm not checking the captcha it's responding back for requirement of the captcha. 但是在我的托管网址**。firebaseapp.com上,如果我没有检查验证码,它会回馈对验证码的要求。 But when I'm checking the recaptcha and sending the request to the server it's giving an error code of 500. 但是,当我检查Recaptcha并将请求发送到服务器时,它给出的错误代码为500。

This is console for my functions in my main project console: 这是主项目控制台中用于我功能的控制台: 在此处输入图片说明

EDIT: Now I've used axios instead of request 编辑:现在我已经使用axios而不是请求

New code: 新代码:

 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) ; }) ; }) ; 

Now its giving following error: Error occurred in axios 现在,出现以下错误: 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 . . . . }

So, I was able to solve this issue by changing the verfication url ie www.google.com/recaptcha/api/siteverify which is not meant for firebase project only for node.js on checking reCaptcha for firebase link here 因此,我能够通过更改验证网址(即www.google.com/recaptcha/api/siteverify来解决此问题,这不适用于仅针对node.js的firebase项目,有关在此处检查reCaptcha for firebase 链接的问题


Also changing my previous answer to this one: 同时将我以前的答案更改为:

 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 }) ; 

and deploying it on the firebase it finally worked. 并将其部署在Firebase上终于可以使用了。 I think firebase was not allowing to make call to the older recaptcha verfication address for which I needed a paid plan but on searching for firebase with recaptcha solved my issue. 我认为firebase不允许致电我需要付费计划的较旧的Recaptcha验证地址,但在使用Recaptcha搜索Firebase时解决了我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> ) at./src/context/AuthContext.js</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at ./src/context/AuthContext.js VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous> ) - VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 语法错误:在 JSON 中的意外令牌 u 在 position 0 在 JSON.parse - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous> )</anonymous> - SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM