簡體   English   中英

Javascript 錯誤:SyntaxError: JSON.parse: JSON 數據第 1 行第 1 列的數據意外結束

[英]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.

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