简体   繁体   中英

JSON object not converting into javascript correctly , JSON sent as a call back from php to AJAX

I've been looking for days and reading close questions and answers trying to solve this issue but not much luck...

I've used json_encode($Final_info); to make a JSON object out of a php array as a return to an AJAX function like so

     <?php .
    .
    .
    $json = json_encode($Final_info);
                    echo $json;
... ?>

and the AJAX function is like so

$.ajax({
         type: "POST",
         url : ajaxurl,
         data :sent_array , 
         success :
         function(Final_info){
**var Final_info = JSON.parse( Final_info );**
//rest of code
}

when I try to parse the JSON object to use the contents of the array in javascript it shows me this error

Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I also would like to note that in the php code I can print the array just fine and also when I alert the JSON object in the JSON format it doesnt show errors

in php 
Array
(
    [prov_fname] => someone
    [prov_lname] => someone
    [prov_email] => someone@gmail.com
    [prov_gender] => female
    [prov_age] => 2
    [prov_phone] => 700000
)

JSON :     {"prov_fname":"someone","prov_lname":"someone","prov_email":"someone@gmail.com","prov_gender":"female","prov_age":"2","prov_phone":"700000"}

I've found that the answer was to simply add async:false to the ajax call, hope this helps someone out!

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