0

I want to parse json data created by PHP json_encode in my front end app, i can't do this:

var data= '<?php echo $jsonarr; ?>';

As it is a Cordova app. Here is my php code:

 <?php
 $arr_login= array("lname"=>"$rowlname","email"=>"$rowemail","fname"=>"$rowfname","mobile"=>"$rowmobile");
  echo json_encode($arr_login);
 ?>

My ajax code:

   $.ajax({
   type:"POST",
   url: url,
   data:data,
   success: function(data){
   var res = $.trim(data);
   if (res == "Password is inccorrect" || res== "Email is inccorrect") {
    $("#errmsg").html(data);
   }
     else{
     var response= JSON.parse(data);
     alert(response);
     //window.open("dashboard.html?sess=logined&","_self");

     }
 }

     });

Now if I alert data it gives me the valid JSON format as sent by PHP, but I need to convert it to javascript object so I can use it in DOM but JSON.parse gives this error in the console:

 VM236:14 Uncaught SyntaxError: Unexpected token / in JSON at position 147
3
  • 1
    What does console.log(data) show you? I mean the raw response, before parsing. Commented Apr 30, 2020 at 17:34
  • 1
    can you please console.log response and share the api response? Commented Apr 30, 2020 at 17:35
  • thanks @vitkarpov this help me, the console show two of ?> that is ?> ?> I typed this twice in my PHP code making the JSON data an invalid format. Fixed it already thanks:) Commented Apr 30, 2020 at 18:20

2 Answers 2

1
var response = JSON.parse(JSON.stringify(data));
Sign up to request clarification or add additional context in comments.

1 Comment

wouldn't this turn it to a string?, what is needed is a javascript object. Solved the issue anyways, error was parse syntax in PHP code having double of this ?> at end of file
1

Why do you alert all the json variable which is definetely an object? I have simulated your process, as @Anik Anwar 's mention JSON.stringify is the solution

    <?php

        $rowlname = "row";
        $rowemail = "email";
        $rowfname = "rowfname";
        $rowmobile = "rowmobile";

        $arr_login= array("lname"=>"$rowlname","email"=>"$rowemail","fname"=>"$rowfname","mobile"=>"$rowmobile");
        $jsonarr = json_encode($arr_login);

    ?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>


    <script src="https://code.jquery.com/jquery-3.5.0.js"></script>

    <script>
        var data = '<?php echo $jsonarr ?>';
    </script>

    <script>
        $.ajax({
            type: "POST",
            url: '/',
            data: data,
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                var res = $.trim(data);
                var response = JSON.parse(JSON.stringify(data));
                window.alert(response.lname);
            }

        });
    </script>

</body>

</html>

4 Comments

yeah, returning the object seems awkward, but i was doing this as part of debugging the error as JSON.pare() suppose to do the work, but it wasn't so i wanted to find out if actually an object is returned or not. I Just tried tried stringify it does returns the response as string, but wanted an object. Thanks
If you want it get as json object in the backend side, do not forget to specify data with contentType: 'application/json', otherwise it's content type multipart/form-data
Isn't that meant to be auto detected?. I didn't put that in backend and am using the returned response without issues. How about that?
If it is fit and not a issue in process, there is no problem. I just follow that as habbits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.