简体   繁体   English

未捕获的SyntaxError:意外的令牌<ajax调用jsonp

[英]Uncaught SyntaxError: Unexpected token < ajax call jsonp

As the title states, getting this error in Chrome remote debugging. 正如标题所述,在Chrome远程调试中收到此错误。 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. 我试图将ajax请求(jsonp)发送到localhost中的.php文件,然后在扫描QR码后使用QR码中的URL对数据库执行某些操作。 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. 我知道jsonp与json不同并使用不同的语法,但是我使用的代码用于其他ajax调用。 I am unable to figure out the problem, and would appreciate some help. 我无法弄清楚问题,并希望得到一些帮助。

Here are the codes: 以下是代码:

.html file .html文件

<!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文件

<?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: 错误是:
Fatal error : 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 C:\\xampp\\htdocs\\MP\\appqrcode.php on line 14 致命错误 :未捕获异常'mysqli_sql_exception',消息'重复条目'12'用于C:\\ xampp \\ htdocs \\ MP \\ appqrcode.php中的键'PRIMARY':14堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ MP \\ appqrcode.php(14):在第14行的C:\\ xampp \\ htdocs \\ MP \\ appqrcode.php中抛出mysqli_stmt-> execute()#1 {main}

Solved the problem by changing the primary key of the table. 通过更改表的主键解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM