简体   繁体   中英

Uncaught SyntaxError: Unexpected token < ajax call jsonp

As the title states, getting this error in Chrome remote debugging. I am trying to send an ajax request (jsonp) to my .php file in localhost which will then do something to the database using the URL in the QR Code after a QR code is scanned. However, I am getting this error.

I am aware that jsonp is different from json and uses different syntax, however the code I am using worked for other ajax calls. I am unable to figure out the problem, and would appreciate some help.

Here are the codes:

.html file

<!DOCTYPE html>
<html>
    <head>
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <title></title>
    </head>
    <body>

        <div data-role="page" id="home">
            <div data-role="header">
                <h1></h1>
            </div>

            <div data-role="main" class="ui-content">
                <p>
                    <a target="_blank" href="javascript:scan();" style="text-decoration: none"><button>Scan</button></a>
                </p>
            </div>
        </div>

        <div data-role="page" id="display">
            <div data-role="header">
                <h1>Display</h1>
            </div>

            <div data-role="main" class="ui-content">
                <table data-role="table" data-mode="column" id="allTable" class="ui-responsive table-stroke">
                    <thead>
                        <tr>
                            <th>Name</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>

        <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        <script type= "text/javascript" src="js/jquery-3.1.1.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script>
            function scan()
            {
                cordova.plugins.barcodeScanner.scan(
                    function (result) {
                        if(!result.cancelled)
                        {
                            if(result.format == "QR_CODE")
                            {
                                var value = result.text;

                            $.ajax({
                                    type: "GET",
                                    url: value + '?callback=?',
                                    dataType: 'JSONP',
                                    async: false,
                                    jsonp : "callback",
                                    jsonpCallback: "jsonpcallback",

                                    success: function jsonpcallback(response)
                                    {
                                        if (response == "Success") 
                                        {
                                            alert(response);
                                        } 
                                        else
                                        {
                                            alert(response);
                                        }
                                    }
                                  });

                            }
                        }
                    },
                    function (error) {
                        alert("Scanning failed: " + error);
                    }
               );
            }
        </script>
    </body>
</html>

.php file

<?php

header('Content-Type: application/json');

require 'dbcon.php';

session_start();

$acc_points = $_SESSION["acc_points"];
$acc_id = $_SESSION["acc_id"];

$result = $con->prepare(" UPDATE `points` SET `acc_points` = acc_points+1  WHERE `acc_id` = ? ");
$result->bind_param("i", $acc_id);
$result->execute();

if($acc_points != null)
  {
      $response = "Success";
      echo $_GET['callback'] . '(' . json_encode($response) . ')';
  }
  else
  {
      $response = "Failed. Please try again.";
      echo $_GET['callback'] . '(' . json_encode($response) . ')';
  }  



    //connection closed
    mysqli_close ($con);

?>

Error was:
: Uncaught exception 'mysqli_sql_exception' with message 'Duplicate entry '12' for key 'PRIMARY'' in C:\\xampp\\htdocs\\MP\\appqrcode.php:14 Stack trace: #0 C:\\xampp\\htdocs\\MP\\appqrcode.php(14): mysqli_stmt->execute() #1 {main} thrown in on line :未捕获异常'mysqli_sql_exception',消息'重复条目'12'用于C:\\ xampp \\ htdocs \\ MP \\ appqrcode.php中的键'PRIMARY':14堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ MP \\ appqrcode.php(14):在第行的抛出mysqli_stmt-> execute()#1 {main}

Solved the problem by changing the primary key of the table.

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