gpt4 book ai didi

php - 语法错误: Unexpected token C in JSON at position 0 in AngularJS

转载 作者:行者123 更新时间:2023-11-29 19:12:56 25 4
gpt4 key购买 nike

我想使用AngularJS将数据保存到MySQL。但是我遇到了问题并且没有。正在添加数据。同时还添加了一个空行。先空行,然后数据。 enter image description here

插入.html

<ion-view view-title="Dashboard">
<ion-content class="padding">
<form ng-submit="submit()">
<div class="list">
<label class="item item-input">
<span class="input-label">Name</span>
<input type="text" ng-model="data.name">
</label>
<label class="item item-input">
<span class="input-label">Surname</span>
<input type="text" ng-model="data.surname">
</label>
<label class="item item-input">
<span class="input-label">Mail</span>
<input type="text" ng-model="data.mail">
</label>
</div>
<button class="button button-block button-positive" type="submit" name="submit" value="Submit to server">
Register
</button>
</form>
</ion-content>
</ion-view>

controller.js

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope,$http) {



$scope.data = {};


$scope.submit = function(){
$http.post("http://localhost:8080/insert.php", {
'name' : $scope.data.name,
'surname': $scope.data.surname,
'mail' : $scope.data.mail
}).then(function(response){
console.log("Data Inserted Successfully");
},function(error){
alert("Sorry! Data Couldn't be inserted!");
console.error(error);

});
}


})

插入.php

<?php

require_once 'conn.php';

$data = json_decode(file_get_contents("php://input"));
// Escaping special characters from submitting data & storing in new variables.
$name = mysqli_real_escape_string($conn, $data['name']);
$surname = mysqli_real_escape_string($conn, $data['surname']);
$mail = mysqli_real_escape_string($conn, $data['mail']);


// mysqli insert query
$query = "INSERT into user (name,surname,mail) VALUES ('$name','$surname','$mail')";
// Inserting data into database
mysqli_query($conn, $query);
echo true;


?>

我正在等待你的帮助

最佳答案

我像这样更改了controller.js。没有添加空行。 enter image description here

但是我不断遇到同样的错误(SyntaxError: Unexpected token C in JSON atposition 0)。我仍然不明白为什么:)。我现在解决了这个问题。但是这个错误仍然困扰着我。我是开放的提案。谢谢

controller.js

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope,$http) {

$scope.data = {};


$scope.submit = function(){



$http({
method: 'POST',
url: 'http://localhost:8080/kaydet.php',
data: {
'name' : $scope.data.name,
'surname' : $scope.data.surname,
'mail' : $scope.data.mail

},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }

});
}

})

关于php - 语法错误: Unexpected token C in JSON at position 0 in AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42945355/

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