Below is the code for my Struts action:
@Action("/trylogin")
@ParentPackage("json-default")
@Result(type = "json", params = { "includeProperties", "msg, productsList" })
public class Login extends ActionSupport {
private static final long serialVersionUID = 1L;
private String utilisateur;
private String motdepasse;
private String msg;
private ArrayList<Article> productsList = new ArrayList<Article>();
@Autowired
private Dao dao;
public String execute() {
if (dao.validCredentials(utilisateur, motdepasse)) {
System.out.println("USER FOUND");
productsList = dao.fetchProducts();
msg = "success";
} else {
System.out.println("ERROR");
msg = "error";
}
return ActionSupport.SUCCESS;
}
public ArrayList<Article> getProductsList() {
return productsList;
}
public String getMsg() {
return msg;
}
Next is my ajax post :
$.post({
url: "trylogin",
data: {
utilisateur: name,
motdepasse: password
}
}).done(function(data) {
console.log(data.productsList.length);
}).fail(function( jqXHR, textStatus ) {
console.log("Fail");
})
I am facing an issue with fetching my productsList. Even though it is loaded properly in my Action, when I try to access data.productsList
in JavaScript, it appears to be empty.
Can anyone guide me on how to successfully retrieve the productsList from my struts action to my javascript?
The productsList contains objects with attributes like name, id, color, etc.