Charith De Silva
Charith De Silva

Reputation: 3730

Dojo /JSON post request on method with multiple parameters?

This should be basics but I'm having trouble with posting multiple parameters from dojo on a rest endpoint. I have the following method on my back end exposed via resteasy.

@POST()
@Path("/updateProduct")
@Consumes(MediaType.APPLICATION_JSON)
public void updateGeneralSettings(String session,Product product) {
    System.out.println("session"+session);
    System.out.println("product"+product.toString);
}

This works perfectly fine just with Product as the parameter. I'm yet to figure out how to build a jason string with another parameter. Product data just binding from form and this is some additional parameter I wanted to attach with it (i.e. session).

jsonData = dojo.toJson(product)

var handler = request.post(url, {
    data: jsonData,
    headers: {
        "Content-Type": 'application/json; charset=utf-8',
        "Accept": "application/json"
    }
});

Appreciate if you guys can give me some solution.

Upvotes: 3

Views: 3386

Answers (1)

condit
condit

Reputation: 10962

Try adding parameter names to your method signature:

public void updateGeneralSettings(@FormParam("session") String session, @FormParam("product") Product product)

and then something like:

var handler = request.post(url, {
    data: {
      session: session,
      product: jsonData
    },
    headers: {
      "Content-Type": 'application/json; charset=utf-8',
      "Accept": "application/json"
    }
});

Upvotes: 3

Related Questions