这是我的角度代码。表单提交代码。单击“提交”按钮时。 JSON Parse error: Unrecognized token '<'
此错误将显示。空记录将保存在DB上。我也为此添加了html代码和PHP服务器端代码。
$scope.submitForm = function() {
$http({
method : 'POST',
url : 'http://localhost/youtubewebservice/checkOutt.php',
data : $scope.user,
dataType: 'json',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
$scope.errorinputFName = data.errors.inputFName;
$scope.errorinputLName = data.errors.inputLName;
}
});
};
Html代码
<form name="userForm" ng-submit="submitForm()">
<div class="form-group">
<label>Name</label>
<input type="text" name="inputFName" class="form-control" ng-model="user.inputFName">
<span ng-show="errorName">{{errorName}}</span>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="inputLName" class="form-control" ng-model="user.inputLName">
<span ng-show="errorEmail">{{errorEmail}}</span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<div id="sendmessageresponse"></div>
</form>
** PHP代码**
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json;charset=UTF-8");
$data = json_decode(file_get_contents("php://input"));
$inputFName = mysql_real_escape_string($data->inputFName);
$inputLName = mysql_real_escape_string($data->inputLName);
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('look4com_lk', $con);
$qry_em = 'select count(*) as cnt from checkout where chkID ="' . $chkID . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if ($res['cnt'] == 0) {
$qry = 'INSERT INTO checkout (inputFName,inputLName) values ("' . $inputFName . '","' . $inputLName . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
$arr = array('msg' => "User Created Successfully!!!", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
} else {
$arr = array('msg' => "", 'error' => 'Error In inserting record');
$jsn = json_encode($arr);
print_r($jsn);
}
} else {
$arr = array('msg' => "", 'error' => 'User Already exists with same email');
$jsn = json_encode($arr);
print_r($jsn);
}
答案 0 :(得分:1)
此特定解析错误表示输出无效JSON(duh)。由于您的输出格式为json_encode
,因此应该是。{ (虽然我通常只对数组使用print_r
...)但是......
我的经验告诉我你的服务器产生错误/通知,php通常输出一些html,因此<
。既然您声称响应是干净的JSON,我建议您查看服务器发送的实际响应。我的假设是,你从命令行测试了你的php脚本(因此php://input
?),但服务器可能处理的请求与你预期的不同。
您可以在几乎所有不错的浏览器的网络标签中检查服务器的响应,通常是F12 - &gt;网络选项卡,然后重新加载和/或重新发送表单。如果你在解决你的php问题时遇到问题,请在你的问题中添加php错误信息或者问一个新问题。
最后一点:请避免使用mysql_*
函数(不推荐使用的mysql库),并使用mysqli_*
函数(mysqli库)或PDO。也可以使用准备好的陈述。
答案 1 :(得分:-1)
我发现了这段代码的错误。这段代码工作正常。任何人都有任何疑问。感谢
html代码
<div ng-controller="ProductController">
<form name="userForm" ng-submit="submitForm()">
<div class="form-group">
<label>Name</label>
<input type="text" name="inputFName" class="form-control" ng-model="user.inputFName">
<span ng-show="errorName">{{errorName}}</span>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" name="inputLName" class="form-control" ng-model="user.inputLName">
<span ng-show="errorEmail">{{errorEmail}}</span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<div id="sendmessageresponse"></div>
</form>
</div>
** PHP代码**
$data = json_decode(file_get_contents("php://input"));
$inputFName = mysql_real_escape_string($data->inputFName);
$inputLName = mysql_real_escape_string($data->inputLName);
//localhost
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('look4com_lk', $con);
$qry_em = 'select count(*) as cnt from checkout where inputFName ="' . $inputFName . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);
if ($res['cnt'] == 0) {
$qry = 'INSERT INTO checkout (inputFName,inputLName) values ("' . $inputFName . '","' . $inputLName . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
$arr = array('msg' => "User Created Successfully!!!", 'error' => '');
$jsn = json_encode($arr);
print_r($jsn);
} else {
$arr = array('msg' => "", 'error' => 'Error In inserting record');
$jsn = json_encode($arr);
print_r($jsn);
}
} else {
$arr = array('msg' => "", 'error' => 'User Already exists with same email');
$jsn = json_encode($arr);
print_r($jsn);
}
**控制器代码**
$scope.submitForm = function() {
// Posting data to php file
$http({
method : 'POST',
url : 'http://localhost/youtubewebservice/checkOutt.php',
data : $scope.user, //forms user object
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
// Showing errors.
$scope.errorinputFName = data.errors.inputFName;
$scope.errorinputLName = data.errors.inputLName;
//$scope.errorMessage = data.errors.Message;
} else {
$scope.contactmessage = data.contactmessage;
//data: {Name: $scope.Name, Email: $scope.Email, Message: $scope.Message}
}
});
};