如何在$ http post方法中隱藏params。如果我使用數據而不是參數給出的例子,我已經將request.getParameter(「key」)中的近400個服務器端請求獲取器更改爲request.getReader(); 我在這裏有一個情況。我們即將從request.getParameter
遷移到request.getReader
。 原因:從請求URL中刪除表單參數。
從此:http://localhost:8080/myApp/test.do?test1=abc&test2=def
這樣:http://localhost:8080/myApp/test.do
這裏是我所取得的成就。 樣本請求(舊):
$scope.myJson = {
'abc' : '123',
'def' : '456',
};
$http({
method : 'POST',
url : 'test.do',
param : {
'test1' : $scope.myJson,
},
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
}). success(function(data, status, headers, config) {
}).
error(function(data, status, headers, config) {
});
樣品要求新
$scope.myJson = {
'abc' : '123',
'def' : '456',
};
$http({
method : 'POST',
url : 'test.do',
data: {
'test1' : $scope.myJson,
},
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
}). success(function(data, status, headers, config) {
}).
error(function(data, status, headers, config) {
});
,你可以看到我已經改變參數去的數據。
服務器端(舊);
final String myJson= request.getParameter("test1");
服務器端(新):
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) {
e.printStackTrace();/*report an error*/ }
try {
JSONObject jsonObj = new JSONObject(jb.toString());
JSONObject jsonObj1 =jsonObj.getJSONObject("test1");
String test1= jsonObj1.toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
這是處理這個之情況的方式,我們在應用周圍400+服務。任何快速解決方法?
編輯1:
public class myController extends AjaxBaseController {
@Override
public ModelAndView executeAjaxCall(final HttpServletRequest request, final HttpServletResponse response) {
final String test1= request.getParameter("test1");
final InputParam loginParam = JsonUtil.convertPojo(test1,InputParam .class); // POJO class
//other logics
};
}
你缺少Spring MVC中的全部要點。你應該幾乎從不直接使用HttpServletRequest,在Spring控制器中你定義了請求的信息,而Spring完成所有的解析,所以你只需要擔心邏輯。 –