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 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类