I'm trying to test a view that requests a list (in this case names) via xhrGet. I use Dojo Toolkit ( webjars), in a Spring-Boot basic application.
The bellow method returns to curl:
curl -i -X GET "http://localhost:8081/the-good-rest/ch3/xhrgame?game=2"
HTTP/1.1 202
Transfer-Encoding: identity
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sat, 26 Sep 2020 20:35:25 GMT
{"content":{"characters":["Sam","Sony","John","Sheila","Martha","Paul","Britney","Tom","Mark","Bruce"]}}
/*-----------------------------------------------------------------*/
This method returns a json object with an empty list:
/* This is the method in Controller */
@RequestMapping(value="ch3/xhrgame",consumes= {MediaType.ALL_VALUE},produces= {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity>> dojoTraining4(@RequestParam(name="game") int level) {
Map> model = new LinkedHashMap>();
CharactersDto characts = new CharactersDto();
DateFormat df = DateFormat.getDateInstance();
if(level>1)
characts.getCharacters().addAll(new ArrayList(Arrays.asList("Sam","Sony","John","Sheila","Martha")));
if(level>=2)
characts.getCharacters().addAll(new ArrayList(Arrays.asList("Paul","Britney","Tom","Mark","Bruce")));
if(level>=3)
characts.getCharacters().addAll(new ArrayList(Arrays.asList("Ann","Mary","Rose","James","Anthony")));
model.put("content", characts);
HttpHeaders headers = new HttpHeaders();
headers.put("Content-Type", Arrays.asList("application/json"));
headers.put("Transfer-Encoding", Arrays.asList("identity"));
headers.put("Accept-Charset",Arrays.asList("utf-8));
ResponseEntity>> entity = new ResponseEntity >>(model,headers,HttpStatus.ACCEPTED);
return entity;
}
Code dojo:
//However when is called from ajax:
function (event){
var button = dojo.byId("idAdd");
dojo.stopEvent(event);
level = dojo.byId("idLevVal").innerHTML;
var xhrArgs = {
url:"/the-good-rest/ch3/xhrgame?game="+level,
content: {"game":level},
handleAs: "json",
load: function(data){
var d = data.content;
if( d.characters.length>0){
//d.characters.lenght is always 0!!!!!
for(i=0;i
names.push( d.characters[i]);
}
},
error: function(error){
console.log(error);
dojo.byId("messages").innerHTML = "Names load failed";
},
handle: function(ioargs){
console.log("STATUS:" + ioargs);
}
};
dojo.byId("messages").innerHTML= "Loading names...";
var deferred = dojo.xhrGet(xhrArgs);
};
This is the way I've solved the problem
//So finally I'm using this implementation
@RequestMapping(value="ch3/xhrgame1",method=RequestMethod.GET,consumes= {MediaType.ALL_VALUE},produces= {MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity dojoTraining7(@RequestParam(name="game") int level) {
String body = "";
if(level==1)
body = "{\"content\":{\"characters\":[\"Sam\",\"Sony\",\"John\",\"Sheila\",\"Martha\"]}}";
if(level==2)
body = "{\"content\":{\"characters\":[\"Sam\",\"Sony\",\"John\",\"Sheila\",\"Martha\",\"Paul\",\"Britney\",\"Tom\",\"Mark\"]}}";
if(level>=3)
body = "{\"content\":{\"characters\":[\"Sam\",\"Sony\",\"John\",\"Sheila\",\"Martha\",\"Paul\",\"Britney\",\"Tom\",\"Mark\",\"Bruce\",\"Ann\",\"Mary\",\"Rose\",\"James\",\"Anthony\",,\"Priscila\",\"Ellise\",\"Kurt\",\"Daisy\",\"Jack\",\"Russell\"]}}";
HttpHeaders headers = new HttpHeaders();
headers.put("Content-Type", Arrays.asList(MediaType.APPLICATION_JSON_UTF8_VALUE));
headers.put("Accept-Charset",Arrays.asList("UTF-8"));
ResponseEntity entity = new ResponseEntity (body,headers,HttpStatus.OK);
return entity;
}