dongzhengzhong1282 2017-03-22 07:33
浏览 61

SyntaxError:AngularJS中位置0的JSON中的意外标记C.

I want to save data to MySQL using AngularJS.But I ran into a problem and did not.Adding data is happening.Also a blank line is added at the same time.Blank line first, then the data. enter image description here

insert.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);

            });
    }


 })

insert.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;


?>

I'm waiting for your help

  • 写回答

2条回答 默认 最新

  • dongyao5186 2017-03-22 11:24
    关注

    I changed controller.js like this.No blank lines added.enter image description here

    but I keep getting the same mistake(SyntaxError: Unexpected token C in JSON at position 0).I still do not understand why :).I solved the problem for now.But the error is still bothering me.I am open to proposals.thanks

    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' }
    
      });
    } 
    
     })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀