【发布时间】:2017-10-16 08:35:51
【问题描述】:
在 Ajax 页面中获取值
function GetInfoDivision()
{
var data = $("#storeName").val();
$.ajax({
type : "POST",
contentType : "application/json",
url : "hello",
data : JSON.stringify(data),
dataType : 'json',
//timeout : 100000,
success : function(map) {
console.log("SUCCESS: ", data);
display(data);
},
error : function(e) {
console.log("ERROR: ", e);
display(e);
},
done : function(e) {
console.log("DONE");
}
});
但控制器页面获取空值...Ajax 数据值未传递给控制器
@RequestMapping(value="/hello", method = RequestMethod.POST)
public String employeeLogin(ModelMap model, HttpServletRequest request) {
String sname = request.getParameter("storeName");
System.out.println("s="+sname);
shopModel s = new shopModel();
s.setStoreName(sname);
//boolean result = employeeService.employeeLogin(employee);
boolean result =false;
if(result == true){
model.addAttribute("message", "Successfully logged in.");
}
else
{
model.addAttribute("message", "Username or password is wrong.");
}
return "redirect:index.jsp";
}
【问题讨论】:
-
你试过不对你的数据进行字符串化吗?
-
@Ivin Raj:你的“数据”只是一个字符串值,你不能只是 JSON.stringify 那样。要了解,您应该打开 Chrome F12 开发者工具下的“网络”选项卡以查看实际发送的内容。
-
Restcontroller 注解在哪里添加?
-
@HoàngLong 查看我更新的图片
-
@IvinRaj:您的图片显示的是 Source 选项卡,而不是 Network 选项卡。
标签: javascript java jquery ajax spring-mvc