[英]JSON.parse: unexpected character in AngularJS and PHP
AgnularJS version is v1.5.6 AgnularJS版本为v1.5.6
When an user input data and submit the form data will be store in mySQL using PHP. 当用户输入数据并提交表单数据时,将使用PHP将其存储在mySQL中。 but I got error < JSON.parse: unexpected character > in firefox.
但我在Firefox中收到错误<JSON.parse:意外字符>。
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 在我的DB_formSave.php中,我有整个代码,例如创建表..插入查询..但是首先,我要确保它是连接数据库和angularJS
Plus, anyone can tell me php can only aceept JSON format or Object? 另外,任何人都可以告诉我php只能使用aceept JSON格式或Object? and AngulrJS can only accpet JSON or Object?
和AngulrJS只能接受JSON或对象? I am pretty new so please share your knowledge to me.
我很新,请与我分享您的知识。
controller.js 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 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\n";
?>
You can try in you php for json decoding. 您可以尝试使用php进行json解码。
$post_date = file_get_contents("php://input");
$data = json_decode($post_date, true);
I sloved I just delete 2 below lines 我觉得我只删除下面两行
header("Access-Control-Allow-Origin: *"); header(“ Access-Control-Allow-Origin:*”); header("Content-Type: application/json; charset=UTF-8");
header(“ Content-Type:application / json; charset = UTF-8”);
anyone can explan what it does? 任何人都可以解释它的作用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.