简体   繁体   中英

Javascript error: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

I have a CS:GO betting website and when I try to go to the page to withdraw skins or something like that after I verify that i am not a robot using recaptcha I get this error:

Javascript error: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

Here is the code :

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

Here is my syntax error:

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)

Network tab results: http://prntscr.com/b1pao5

Please post in your question the JSON you are receiving from the server.

This issue could be related to a malformed JSON, or a wrong Header Content Type of your request, which actually should be as "application/json" as "Content-type" for JSON.

I solved the issue by generating a recaptcha key and adding it to the config file. After that the problem got solved.

Define dataType json in your ajax call and then you don't need to use 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);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM