Q
Q
Quber2014-11-26 20:42:54
JavaScript
Quber, 2014-11-26 20:42:54

Why is AngularJS sending invalid JSON to the server?

I am using AngularJS in my project.
Question 1. I make a request like:

$scope.user.id = 31;
$scope.user.name = "Vladimir";

$http.post(url, $scope.user);

Next, Angular sends Json to the server like this:
{ id:31, name="Vladimir" }
Of course, Json does not pass validation on the server side and throws an error to the client. After all, it would be correct like this:
{ "id":31,"name"="Vladimir" }
The following method helps here:
$http.post(url, angular.toJson($scope.user));
In this case, the request sends valid Json.
But shouldn't Angular serialize data out of the box before sending it to the server?
=========================================
Question 2. I use RestAngular. It's the same there:
$scope.user = Restangular.one("user", 31);
$scope.user.name = "Vladimir";
$scope.user.patch();

Json is sent in the same way as in the previous example:
{ id:31, name = "Vladimir" }
How to pass an array in this case, as in the example below?
{ "user": {"name"="Vladimir", "id"=31 }}
 

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Musin, 2014-11-26
@vmpartner

The only correct format is:
json.org/example
Try using the stable version of Angular

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question