最近一个项目,客户提供的接口API规定使用get请求访问接口,参数放到body中传递。虽然很奇葩,但是客户接口这么做的,我们也只能给安排了。
调查了一下使用XMLHttpRequest是不行的,也就意味着ajax没戏。试了一下nodejs的request模块,是可以的。
废话不多说,看代码吧。
前端:
var request = require('request');
var url="你的地址";
var requestData={test:[1,2,3]};
request({
url: url,
method: "GET",
json: true,
headers: {
"content-type": "application/json",
},
body: {test:[1,2,3]}
}, function(error, response, body) {
//判断是否请求成功
if (!error && response.statusCode == 200) {
console.log(body)
}
});
java后端:
@RequestMapping(path="/asrmanager/audiototext/{recognitionHandle}/result",method = RequestMethod.GET)
@ResponseBody
public String getAudiototext(@RequestBody Options options,HttpServletRequest requset ) {
return "success";
}
Options :
import java.util.List;
public class Options {
private List<String> test;
public List<String> getTest() {
return test;
}
public void setTest(List<String> test) {
this.test = test;
}
}