Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

php - Why am I getting this invalid parse?

I have an Ajax function to load all columns names and their values via php as follow:

PHP SIDE:

$res = $db->query($stmt);
$rsltz= array();
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
    foreach ($row as $colonne => $valeur) {
      $rsltz[$colonne] = isset($valeur) ? $valeur : ""; 
    }
}
header('Content-type: application/json');
echo (json_encode($rsltz));

AJAX FUNCTION:

function jet(a,b,c){
$.ajax({
        type: 'POST',   
        cache: false,           
        url: 'builder.php',
        data:  { typ:a, lg:b ,menu:c},
         success: function(result) { 
             if(typeof(result) === "string") {
                $('#par').after(result);
              } else {
                //JSON hier comes a loop to display key=>val
                var pars = JSON.parse(result); 
             $('#par').after(pars.datasrc);
              }          
        },
        error: function (jqXHR, textStatus, errorThrown) {
        alert(jqXHR+'
'+textStatus+'
'+ errorThrown);
        }
});
}

I have this error: enter image description here

here the json.encoded result :

{"id":"510","menu":"regis","taketo":"regis1","auth":"ALL"}

I do appreciate any help ?.

question from:https://stackoverflow.com/questions/65937587/why-am-i-getting-this-invalid-parse

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We don't have to re-parse what is already parsed! The response (result in Ajax function) IS already parsed. So just use it as it is:

var pars = JSON.parse(result); 
$('#par').after(pars.datasrc);

has to be replaced with

$('#par').after(result.datasrc);

Thank you guys.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...