我正在尝试制作一个带有参数的angularjs $ http.get请求,但由于义务而无法正常工作。请你确认我做错了什么,也许语法在AngularJS呼叫或Java API方法中是错误的
$http.get(document.requestPathPrefix + "/home/my-api",
$scope.requestParametersObject
).then(
function(response) {
$scope.output = response.data;
},
function(response) {
$scope.retrieveErrorMssg = "Failed to retrieve required data.";
});
我的参数就像在此图像中
API调用的参数对象
和在Java API中像这样
@RequestMapping(value = "/my-api", method = RequestMethod.GET)
public ResponseEntity<Collection<MyObjectResponse>> getMyObjResponse(@RequestBody final MyObjectRequest request)
{
Map<Integer,MyObjectResponse> oResponse = new HashMap<>();
try {
//Manipulation and db calls
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return new ResponseEntity<Collection<MyObjectResponse>>(oResponse.values(), HttpStatus.OK);
}
尝试,
$http({
url: '/home/my-api',
method: "GET",
headers: {
'Content-type': 'application/json'
},
data: JSON.stringify(request)
}).success(function(data, status, headers, config) {
}).error(function(data, status, headers, config) {
return null;
});
如果您担心语法,请查看这个简单的示例并将其修改为您的需要。
在JS中,您的http.get调用应包含URL及其参数,如果有的话,
$http.get('getUserInfo',
{
params : {
'userEmail' : 'userEmail'
}
}).then(function(response) {
console.log(response);
});
如果将参数传递给API调用,请确保您的Java控制器或服务的参数定义为接收相同的参数。理想情况下,参数应匹配呼叫和提供商
例如,对于上述API调用,弹簧控制器应该喜欢以下,
@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
public @ResponseBody User getUserInfo(
@RequestParam("userEmail") String userEmail) {
// Do Something
}