Ionic 目录:https://blog.youkuaiyun.com/dkbnull/article/details/87937179
前面 Ionic前台与PHP后台间数据交互 这篇文章我们说过,AngularJS 封装了一个 $http服务,用来与远程服务器进行数据交互。但是 AngularJS 中的 $http服务使用的Content-Type为application/json,是使用json序列化传参。
也就是说,我们使用 $http.post 请求来与后台进行数据交互时,如果是向后台发送数据,那么发送的数据格式为json。这样如果我们后台使用接收参数的形式来接收数据,就会接收不到数据。
比如,我们ionic前台代码为
$http.post(url, {
app_id: "app_id",
method: "method",
}).success(function (response) {
console.log('success:', response);
}).error(function (response) {
console.log('fail:', response);
});
Java后台代码为
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public Object gateway(
@RequestParam(required = false, value = "app_id") String app_id,
@RequestParam(required = false, value = "method") String method
) { }
我们进行一下测试,可以看到,后台接收到数据为null
所以后台我们要设置接收json格式数据
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public Object gateway(
@RequestBody Map<String, String> requestParams
) { }
这样我们就能接收到ionic前台post到后台的数据了。