dojo ajax .status 0,Ajax with dojo Toolkit + Spring boot Controller : Testing with curl works, from ...

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值