0

I am writing an AJAX function, but it looks like there is an error. I can't seem to find any. Please help! I have browsed these questions: this, this, and this but none of it is working. Here is my code:

      function registerDevice(fp,uid)
  {
    $.ajax({
         type: "POST",
         async: true,
         url: "backend.php",
         data: {"fp": fp, "uid" : uid},
         success: function(output) {
          var json = eval('(' + output + ')');
          var status = json['status'];
          if(status == "success")
          {
            //redirect user.
            setTimeout(function(){
                    //do what you need here
                    <?php
                    if(isset($_GET['goto'])) 
                      {
                        echo 'window.location.replace("'.$_GET["goto"].'");';
                      }
                      else
                      {
                        echo 'window.location.replace("index.php");';         
                      }
                    ?>
            }, 2000);
          }
          else
          {
            $("#login-msg").text("Something went wrong. Please try again.");
            $("#login-alert").fadeTo(4000, 500).slideUp(500, function(){
            $("#login-alert").hide();
            });
          }
         },
         error: function(error) {
          $("#login-msg").text("There was an unknown error.");
          $("#login-alert").fadeTo(4000, 500).slideUp(500, function(){
          $("#login-alert").hide();
        });
         }
    });
  }

and it is called like this:

    $('body').on("click", "#mybtn", function (e) {
registerDevice(result,uid);   
});

Here is the error I get in Google Chrome:

VM411:1 Uncaught SyntaxError: Unexpected token )
success @   login_alpha.php:344
j   @   jquery.js:3148
fireWith    @   jquery.js:3260
x   @   jquery.js:9314
b   @   jquery.js:9718

P.S: line 344 refers to this line:

var json = eval('(' + output + ')');

Edit 1: backend.php

<?php
$uid = $_POST['uid'];
$fp = $_POST['fp'];

//my code here.
$response_array['status'] = "success";
$response_array['type'] = "cookie";

?>
Community
  • 1
  • 1
Aekansh Dixit
  • 513
  • 1
  • 9
  • 20

2 Answers2

0

eval function needs a string to evaluate. You are trying to eval a JSON Object I think.

Try:

var json = eval('(' + JSON.stringify(output) + ')');

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

Asim K T
  • 16,864
  • 10
  • 77
  • 99
0

I found the solution, and it was very silly.

The backend.php was NOT giving a JSON response! This was the original code:

<?php
$uid = $_POST['uid'];
$fp = $_POST['fp'];

//my code here.
$response_array['status'] = "success";
$response_array['type'] = "cookie";

?>

I just added this line:

echo json_encode($response_array);

This made sure I was sending back a JSON response to the AJAX call's success function! What a stupid error.

Final edit for backend.php:

<?php
$uid = $_POST['uid'];
$fp = $_POST['fp'];

//my code here.
$response_array['status'] = "success";
$response_array['type'] = "cookie";

//print json response
echo json_encode($response_array);
?>
Aekansh Dixit
  • 513
  • 1
  • 9
  • 20