dtcyv3985 2017-09-26 00:18
浏览 117

JSON.parse:AngularJS和PHP中的意外字符

AgnularJS version is v1.5.6

When an user input data and submit the form data will be store in mySQL using PHP. but I got error < JSON.parse: unexpected character > in firefox.

in my DB_formSave.php, I have whole code such as creating table.. insert query.. but first, I want to make sure it's connection db and angularJS

Plus, anyone can tell me php can only aceept JSON format or Object? and AngulrJS can only accpet JSON or Object? I am pretty new so please share your knowledge to me.

controller.js

var userControllers = angular.module('userControllers', []);


userControllers.controller('UserFormController', ['$scope', '$http', 
'$location', '$window', 'myFactory', function ($scope, $http, $location, 
$window, myFactory) {
var dataObj = "";
var config = { headers: { 'Content-Type': 'application/x-www-form urlencoded;charset=utf-8;' } }

$scope.submitForm = function (user) {
    myFactory.set(user);  // Set data to myFactory to share with ConfirmationController        
    dataObj = {
        "name": user.name, 
        "province": user.province, 
        "telephone": user.telephone,
        "postalcode": user.postalcode, 
        "salary": user.salary
    }
    console.log(dataObj);   //it shows this format like below :

      Object { name: "Jason", province: "Québec", telephone: "(416) 123-1234", postalcode: "M2N3L3", salary: "40.000" }

    $http.post('ajax/DB_formSave.php', dataObj)
    .success(function(data) {
        console.log(data);
        console.log(dataObj);
    })
  }

}]);

DB_formSvae.php

<?php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$post_date = file_get_contents("php://input");
$data = json_decode($post_date);

$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "mydb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// check connection
if($conn->connect_error){
die("Connection failed:".$conn->connect_error);
}
echo "Connected successfully
";

?>
  • 写回答

2条回答 默认 最新

  • doutang1856 2017-09-26 02:35
    关注

    You can try in you php for json decoding.

    $post_date = file_get_contents("php://input");
    $data = json_decode($post_date, true);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘