1

I must create a POST with AJAX request in JQuery, where I must pass an array of objects that I can create with:

var actor=new Array();
for(var i=1;i<=incr;i++)
{
    actor.push({"name": document.getElementById("idAN"+i).value,
        "surname": document.getElementById("idAS"+i).value,
        "dateborn": document.getElementById("idAB"+i).value,
        "gender": document.getElementById("idAG"+i).value,
        "movie": datas});
    alert("actorX: "+actor[i-1].surname);
}
$.ajax({
    method:'POST',
    dataType: 'json',
    data:{ actors: actor },
    url:'http://localhost:8080/movies/actors',
        success: function (rest) {
                alert("aggiunto attore");
    },
    error: function(rest){
        alert("non aggiunto attore");
    }
});

I receive the data with this Java method, but this doesn't work. anyone can help me?

@RequestMapping(value = "movies/actors", method = RequestMethod.POST)

public ArrayList<Actor> addActors(@RequestBody Actor[] actors) {...}

After three days of work, i solve this with the help from the comments. This is the result of the method in java:

@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping(value = "movies/actors", method = RequestMethod.POST,headers="Accept=application/json")

public @ResponseBody ArrayList<Actor> add (@RequestBody Actor[] actors) {
    //Actor[] actors = actobj.getAllActors();
    ArrayList<Actor> json = new ArrayList<Actor>();
    for(Actor A : actors){
        System.out.println("Arrivo");
        serv.addActor(new Actor(A.getName(),A.getSurname(),A.getBorn(),A.getGender(),A.getMovie()));
        System.out.println("nomeAttore"+A.getName());
         json.add(A);
    }
    return json;
}

and this is the post request:

$.ajax({
    method:'POST',
    dataType: 'json',
    data:JSON.stringify(actor),
    contentType: "application/json; charset=utf-8",
    url:'http://localhost:8080/movies/actors',
    success: function (rest) {
         alert("aggiunto attore");
    },
    error: function(rest){
        alert("non aggiunto attore");
    }
});

In particular, i changed a value of a parameter in the query sql from databorn to born, because i have the method getBorn and setBorn, the name must be eguals; and eguals must be the name of the object's parameter in the array actor in javascript: born

3
  • do you have some error? Commented Jan 14, 2016 at 10:45
  • yes, i have the error 415, unsopported media type Commented Jan 14, 2016 at 10:53
  • look into this and this answers Commented Jan 14, 2016 at 14:37

2 Answers 2

0

Do you get any errors? I think the problem can be that you are not serializing your data to JSON format, try:

data:{ actors: JSON.stringify(actor) }

Also add:

contentType: "application/json; charset=utf-8",

should be ok.

EDIT

try

data: JSON.stringify(actor),
Sign up to request clarification or add additional context in comments.

27 Comments

I already tried this solution but it seems that's not working, it gives 400 bad request, I think probably because these are not simple objects (actors) but are custom objects
Check eddited answer
i have this response from the console:
Response for preflight has invalid HTTP status code 403
Failed to load resource: the server responded with a status of 403 (Forbidden)
|
0

How about changing

@RequestMapping(value = "movies/actors", method = RequestMethod.POST)

to

@RequestMapping(value = "movies/actors", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)

jackson dependency must be added for this, if it is a maven project try add following as dependencies.

 <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.6.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.6.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.6.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-hibernate4</artifactId>
      <version>2.6.1</version>
    </dependency>

4 Comments

on APPLICATION_JSON_VALUE, i have an error, i must put a particular dypendence for it?
yup, add jackson dependies to the pom.xml(if it is maven project).
yes, i have maven project. i tried, but i have the same error: APPLICATION_JSON_.. cannot be resolved or is not a type
Try the solution in this answer stackoverflow.com/questions/33647939/…

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.