![](/img/trans.png)
[英]SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data?
[英]Javascript error: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
我有一个 CS:GO 博彩网站,当我在验证我不是使用 recaptcha 的机器人后尝试转到该页面以提取皮肤或类似内容时,我收到此错误:
Javascript 错误:SyntaxError: JSON.parse: JSON 数据第 1 行第 1 列的数据意外结束
这是代码:
function redeem(){
var code = $("#promocode").val();
$.ajax({
url:"/redeem?code="+code,
success:function(data){
try{
data = JSON.parse(data);
if(data.success){
bootbox.alert("Success! You've received "+data.credits+" credits.");
}else{
bootbox.alert(data.error);
}
}catch(err){
bootbox.alert("Javascript error: "+err);
}
},
error:function(err){
bootbox.alert("AJAX error: "+err);
}
});
}
这是我的语法错误:
SyntaxError: Unexpected end of JSON input
at Object.parse (native)
at Object.$.ajax.success (http://www.gamesnodie.com/template/js/offers.js?v=106:249:29)
at j (http://www.gamesnodie.com/template/js/jquery-1.11.1.min.js:1:27244)
at Object.k.fireWith [as resolveWith] (http://www.gamesnodie.com/template/js/jquery-1.11.1.min.js:1:28057)
at x (http://www.gamesnodie.com/template/js/jquery-1.11.1.min.js:1:85993)
at XMLHttpRequest.b (http://www.gamesnodie.com/template/js/jquery-1.11.1.min.js:1:90047)
网络标签结果: http : //prntscr.com/b1pao5
请在您的问题中发布您从服务器接收的JSON。
此问题可能与格式错误的JSON或请求的标题内容类型错误有关,对于JSON,实际上应将其作为“ application / json”与“ Content-type”。
我通过生成一个Recaptcha密钥并将其添加到配置文件中解决了该问题。 之后问题就解决了。
在ajax调用中定义dataType json
,然后就不需要使用JSON.parse()了。
function redeem(){
var code = $("#promocode").val();
$.ajax({
url:"/redeem?code="+code,
dataType: 'json',
success:function(data){
try{
if(data.success){
bootbox.alert("Success! You've received "+data.credits+" credits.");
}else{
bootbox.alert(data.error);
}
}catch(err){
bootbox.alert("Javascript error: "+err);
}
},
error:function(err){
bootbox.alert("AJAX error: "+err);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.