SyntaxError: Unexpected token u in JSON at position 0 at 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) ;
因此,如果用户不检查验证码,它将 return 代码 0,代码 1 表示成功,代码 -1 表示验证失败。此代码在本地主机上完美运行。但是在我的主机上 url **.firebaseapp.com 如果我不检查验证码,它会响应验证码的要求。但是当我检查 recaptcha 并将请求发送到服务器时,它给出了错误代码 500。
这是我在主项目控制台中的功能控制台:
编辑: 现在我使用了 axios 而不是 request
新代码:
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) ;
}) ;
}) ;
现在出现以下错误:
axios 发生错误 </p>
<p>{ 错误:getaddrinfo EAI_AGAIN www.google.com:443
在 GetAddrInfoReqWrap.onlookup [完成时] (dns.js:67:26)
错误号:'EAI_AGAIN',
代码:'EAI_AGAIN',
系统调用:'getaddrinfo',
主机名:'www.google.com',
主持人:'www.google.com',
端口:443
.
.
.
.
}
所以,我能够通过更改验证 url 来解决这个问题,即
www.google.com/recaptcha/api/siteverify
这不适用于 firebase 项目,仅适用于 node.js 检查 firebase 的 reCaptcha link here
同时更改我之前对此的回答:
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 解决了我的问题。
我已将 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) ;
因此,如果用户不检查验证码,它将 return 代码 0,代码 1 表示成功,代码 -1 表示验证失败。此代码在本地主机上完美运行。但是在我的主机上 url **.firebaseapp.com 如果我不检查验证码,它会响应验证码的要求。但是当我检查 recaptcha 并将请求发送到服务器时,它给出了错误代码 500。
这是我在主项目控制台中的功能控制台:
编辑: 现在我使用了 axios 而不是 request
新代码:
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) ;
}) ;
}) ;
现在出现以下错误:
axios 发生错误 </p>
<p>{ 错误:getaddrinfo EAI_AGAIN www.google.com:443
在 GetAddrInfoReqWrap.onlookup [完成时] (dns.js:67:26)
错误号:'EAI_AGAIN',
代码:'EAI_AGAIN',
系统调用:'getaddrinfo',
主机名:'www.google.com',
主持人:'www.google.com',
端口:443
.
.
.
.
}
所以,我能够通过更改验证 url 来解决这个问题,即
www.google.com/recaptcha/api/siteverify
这不适用于 firebase 项目,仅适用于 node.js 检查 firebase 的 reCaptcha link here
同时更改我之前对此的回答:
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 解决了我的问题。