【发布时间】:2012-03-09 00:06:09
【问题描述】:
当我填写登录/注册表单时,服务器会将我发送到 Json 页面。 我尝试将该 json 放入 js/jquery 文件中,而不重定向到该页面。
谁能告诉我如何获取 json 数据?
为了解释我的意思,我向您展示了一些代码 + html 页面。
HTML 格式:
<form id="registerForm" action="http://localhost:8081/rest/users/register" method="post" >
<table>
<tr>
<td>
<label for="firstname" class="normalFont"><span rel="localize[register.firstname]">First name</span></label>
</td>
<td>
<input id="firstname" name="firstname" type="text" class="required">
</td>
<td class="status">
</td>
</tr>
... (some other fields)
<tr>
<td>
<input type="submit" value="Register">
</td>
</tr>
</table>
服务器:
@Override
@RequestMapping(value="/register", method = RequestMethod.POST)
public @ResponseBody Message register(String firstname, String lastname, String username, String password, String email, String address, String zip, String city, String country) {
try
{
User user = new User();
user.setFirstname(firstname);
user.setLastname(lastname);
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
user.setAddress(address);
user.setZip(zip);
user.setCity(city);
user.setCountry(country);
userService.register(user);
return new Message(true, "Registration Succesful");
}
catch (Exception ex)
{
return new Message(false, ex.getMessage());
}
}
提交表单后得到的,是一个新的 HTML 页面:
{"state":true,"content":"Registration Succesful"}
我怎样才能把那个 json 放到我的 js 文件中,而不去这个 html 页面。
【问题讨论】:
-
你可能想看看this 使用
.ajax()。