gpt4 book ai didi

php - 语法错误 : Unexpected token < at Object. 解析( native )

转载 作者:行者123 更新时间:2023-11-29 11:42:16 24 4
gpt4 key购买 nike

我刚刚创建了一个 ionic 应用程序,它只有一个注册页面。我正在做的是在后端使用 php RESTful Web 服务,MySql 是数据库。但我收到此错误:“SyntaxError:意外的标记<”。我不知道如何解决这个错误。请指导我解决此类错误。

我的index.html是:

<ion-content class="has-header" ng-controller="SignupCtrl">
<div class="list list-inset">

<label class="item item-input">
<input class="form-control" type="text" ng-model="userdata.username" placeholder="Enter Username">
</label>

<label class="item item-input">
<input type="text" ng-model="userdata.email" placeholder="Enter Your Email">
</label>

<label class="item item-input">
<input class="form-control" type="password" ng-model="userdata.password" placeholder="Enter Your Password">
</label>

<button class="button button-block button-positive" ng-click="signup(userdata)">SignUp</button><br>
<span>{{responseMessage}}</span>
</div>
</ion-content>

我的 Controller 代码是:

.controller('SignupCtrl', function($scope, $http) {
$scope.signup = function (userdata) {
var request = $http({
method: "POST",
url: "http://localhost/lastLog.php",
crossDomain : true,
data: {
email: userdata.email,
password: userdata.password,
username: userdata.username
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
/* Successful HTTP post request or not */
request.success(function(data) {
if(data == "1"){
$scope.responseMessage = "Successfully Created Account";
}
if(data == "2"){
$scope.responseMessage = "Create Account failed";
}
else if(data == "0") {
$scope.responseMessage = "Email Already Exist"
}
});
}
})

我的 php 代码是:

<?php
header("Content-Type: application/json; charset=UTF-8");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $postdata->email;
$password = $postdata->password;
$username = $postdata->username;

$con = mysql_connect("localhost","root",'') or die ("Could not connect: " . mysql_error());;
mysql_select_db('db_lastLog', $con);

$qry_em = 'select count(*) as cnt from users where email ="' . $email . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);

if($res['cnt']==0){
$qry = 'INSERT INTO users (name,pass,email) values ("' . $username . '","' . $password . '","' . $email . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
echo "1";
} else {
echo "2";;
}
}
else
{
echo "0";
}
?>

最佳答案

SyntaxError: Unexpected token <通常意味着您的 JSON 响应中存在错误,因为错误的位置位于 fromJson (angular.fromJson) 中这意味着它无法解析 JSON 响应。如果你打开network控制台中的选项卡并检查请求,响应可能以 < 开头,一旦你修复了这个错误/json,它应该可以正常工作

关于php - 语法错误 : Unexpected token < at Object. 解析( native ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35575467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com