/ / JSON POST pomocou AngularJS - angularjs, json, post

JSON POST pomocou AngularJS - angularjs, json, post

AngularJS nevytvára JSON podľa potreby. S kódom (nižšie) vytvára pole (serializovaná para), ale nie formu. tj dostávam

 {
data : "value",
date : "value"
}

Ale chcel - JSON tj

{
"data" : "value",
"date" : "value"
}

Kód pre Angular to POST json je (snippet)

<script>
// Defining angularjs application.
var postApp = angular.module("postApp", []);
// Controller function and passing $http service and $scope var.
postApp.controller("postController", function($scope, $http) {
// create a blank object to handle form data.
$scope.user = {};
// calling our submit function.
$scope.submitForm = function() {
// Posting data to php file
$http({
method  : "POST",
url     : "user.php",
data    :JSON.stringify($scope.user),
headers : {"Content-Type": "application/json"}
})
.success(function(data) {
if (data.errors) {
// Showing some error which has to come from server
} else {
$scope.message = data.message; //make the json
}
});
};
});
</script>

Čo by som mal urobiť, aby som získal JSON a nie Array?

odpovede:

0 pre odpoveď č. 1

Ak chcete svoje údaje vo formáte JSON, mali by ste použiť JSON.parse ();

$http({
method  : "POST",
url     : "user.php",
data    :JSON.parse($scope.user),
headers : {"Content-Type": "application/json"}
})

0 pre odpoveď č. 2

vyskúšal si to angular.toJson metóda?


0 pre odpoveď č. 3

Vyriešené. použitím ničoho, tj.

json: $ obor.užívateľ to funguje ...