0

I have a post call in my code:

save2 : function ( lotto ) {

    var configDistintaIngrediente = {
      params: {
              distintaBaseGelato_id : 1,
              ingrediente_id : 2,
              quantitaIngrediente : 4.56,
              lottoIngrediente : lotto } 
      };

      return $http.post(urlDistinteIngredienti, "", configDistintaIngrediente)
        .success(function(data) {
          alert ("OK");
        })
        .error(function(data, status, headers, config) {
              alert (
                "data: " + data + "\n" + 
                "status: " + status + "\n" + 
                "headers: " + headers + "\n" + 
                "config: " + config + "\n" 
              );
        });
  },
  // continue....

I do not why when I call the function, I get this error:

SyntaxError: Unexpected token I
at Object.parse (native)
at fromJson (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:1065:14)
at defaultHttpResponseTransform (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:8579:16)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:8664:12
at forEach (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:323:20)
at transformData (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:8663:3)
at transformResponse (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:9389:23)
at processQueue (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:13189:27)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:13205:27
at Scope.$get.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js:14401:28)

It is very strange because in avery case the operation to insert in my db work fine but there is an error in the post call and in fact I get the alert in:

.error(function(data, status, headers, config) {
              alert (
                "data: " + data + "\n" + 
                "status: " + status + "\n" + 
                "headers: " + headers + "\n" + 
                "config: " + config + "\n" 
              );
        });

This request works but get me an error...

3
  • 1
    You have this error because the server expect something, and the client send something else. The server can't parse the JSON into the param of his function. You have to match this param and the client's JSON Commented Apr 28, 2015 at 9:57
  • Thank you for the answer. In my application I pass parameter to server with an object like { params: { param1 : value1, ....paramN: valueN} }; Yhis object is the third parametet of http.post Commented Apr 28, 2015 at 10:06
  • 1
    This object have to be the exact same thing. If you have multiple value on your JSON, you have to create a new class with the same name. I'll put an example on answer, will be easier. Commented Apr 28, 2015 at 11:44

2 Answers 2

1

Like I said in comment, The server can't match receive data with his parameter.

For example, you have this JSON :

 params: {
              distintaBaseGelato_id : 1,
              ingrediente_id : 2,
              quantitaIngrediente : 4.56,
              lottoIngrediente : lotto } 
      };

You have 2 options to make this match with parameter, send this data one by one, or create a class wich have the exact same declaration.

If you send one by one :

Client

$http.post(urlDistinteIngredienti, "", configDistintaIngrediente.distintaBaseGelato_id , configDistintaIngrediente.ingrediente_id, ... )

And you serveur will just have to get the parameter like this :

Server

function(int distintaBaseGelato_id , int ingrediente_id, ...)

The second option : create a class

You can create a class which have the same declaration that your JSON. To do so, your client stay the same, he sends all the data in the JSON, it's the server who make the match.

Server

function(configDistintaIngrediente value)

configDistingaIngrediente will be a class create for the match.This class will be like this :

//Declaration class
private int distintaBaseGelato_id;
private int ingrediente_id ;
private float quantitaIngrediente;
private String lottoIngrediente ;

// Getters and setters

With this class, the server will be able to match each JSON data, with his parameters data and then you will be able to use this data in your function.

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you very much Apédémak. I am sure that my request is made in the correct way

the call post on the http service is (from documentation):

$http.post (url, data, [config]);

where [config] is an object in js like:

{ params: {
          distintaBaseGelato_id : 1,
          ingrediente_id : 2,
          quantitaIngrediente : 4.56,
          lottoIngrediente : lotto } 
};

In fact, I always have requests like this.

Now I have resolved my problem. The problem is that in my servlet I had a row of code like this:

response.setContentType("application/json")

and I returned simple "text/plain" in fact all worked but I had an error on response.

Thank you very much for your time and your patience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.