未捕获到的SyntaxError:JSON中位置0处的意外令牌 [英] Uncaught SyntaxError: Unexpected token in JSON at position 0

查看:397
本文介绍了未捕获到的SyntaxError:JSON中位置0处的意外令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些帮助下,我设法制作了一个在将用户添加到数据库之前验证并确认用户密码的表单.我遇到了一些问题,因为未发送用户数据,并给我以下错误:

With some help, I have managed to make a form that validates and confirms user password before adding the user to the database. I have encountered some problem as the data of the user is not sent and giving me the following error :

未捕获的SyntaxError:JSON中位置0处的意外令牌
在JSON.parse()中
在Object.success处(confirm3.php:29)
在我(jquery.min.js:2)
在A(jquery.min.js:4)
在 XMLHttpRequest. (jquery.min.js:4)

Uncaught SyntaxError: Unexpected token in JSON at position 0
at JSON.parse ()
at Object.success (confirm3.php:29)
at i (jquery.min.js:2)
at at A (jquery.min.js:4)
at XMLHttpRequest. (jquery.min.js:4)

错误

at Object.success(confirm3.php:29)

at Object.success (confirm3.php:29)

是指下面的这一行

var data = JSON && JSON.parse(response) || $.parseJSON(response);

POST变量

$UserNm=$_POST["UserNm"];
$UserId=$_POST["UserId"];
$UserPwd=$_POST["UserPwd"];

为了清楚起见,应返回的数据是$ReturnMessage,该数据是从存储过程中检索的. $ReturnMessage将同时显示成功和失败操作的操作状态.

To make things clear, the data that should be returned is $ReturnMessage which is retrieved from stored procedure. $ReturnMessage will display the status of the operation for both successful and fail operation.

$ReturnMessage的示例:

已存在用户ID(011码)."
添加了用户ID MINT20."

"USER ID EXIST. (011 CODE)."
"USER ID MINT20 ADDED."

使用POST方法:if(isset($_POST['Submit'])) {

$ ReturnMessage:

 if(isset($ReturnStatus) && $ReturnStatus==1) {
 $ReturnMessage=odbc_result($stmt,'ReturnMessage');
 }
 }

 $ReturnMessage = utf8_encode ($ReturnMessage);
 echo json_encode($ReturnMessage);
 }

脚本:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script type="text/javascript">
$(function() {
  $("#myForm").on("submit", function(e) {
    e.preventDefault(); 
    var password = $("#UserPwd").val();
    var confirmPassword = $("#ConfirmPassword").val();
    console.log(password,confirmPassword)  
    if ($.trim(password) === password && 
      password !== "" && 
      password === confirmPassword) { 
      $.ajax({
        url: "confirm3.php",
        method: "POST",
        data: { Submit: "true" },
        success: function(response) {
          var data = JSON && JSON.parse(response) || $.parseJSON(response);
          alert(data);
        }
        });
        } else {
          alert("Please Enter Password Correctly");
        }
      });
});
<script>

我有点困惑.请指导我.谢谢.

I'm kind of confuse. Please guide me. Thank you.

推荐答案

首先,jQuery可以自动为您解码JSON(它将尽力猜测).手动执行此操作只会使您的代码更加冗长且容易出错.但是,您没有给出任何提示.您共享的代码中没有任何有关您打算使用jQuery的线索.

First of all, jQuery can decode JSON automatically for you (and it will make its best to guess). Trying to do it manually only makes your code more verbose and error-prone. However, you don't give it any hint. Nothing in the code you've shared gives any clue about your intention to use jQuery.

您可以按时间顺序进行的所有阶段:

All the phases where you can do so, in chronological order:

  1. 来自Web服务器的报告数据类型.如果您碰巧正在使用Apache:

  1. Report data type from web server. If you happen to be using Apache:

<Files "confirm3.php">
    ForceType application/json
</Files>

  • 从PHP报告数据类型:

  • Report data type from PHP:

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

  • 告诉jQuery您期望使用JSON:

  • Tell jQuery that you expect JSON:

    url: "confirm3.php",
    method: "POST",
    dataType: "json",
    data: { Submit: "true" },
    success: function(response) {
        console.log(data); // Already decoded (don't use alert to debug!)
    }
    

  • 您当然可以省略几乎所有步骤,但并非全部.

    You can certainly omit almost any step, but not all.

    第二,如果您收到JSON解析错误,则首先需要检查响应是否为有效JSON.最简单的查看方法是使用浏览器开发人员工具,更具体地说,是网络"窗格.

    Secondly, if you get a JSON parse error the very first you need to check is whether the response is valid JSON. The most straightforward way to see it is using the browser developer tools, more specifically the "Network" pane.

    这篇关于未捕获到的SyntaxError:JSON中位置0处的意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆