$('#loginForm').submit(function(e){
var $inputs = $(this).find("input");
var serializedData = $(this).serialize();
$inputs.prop("disabled", true);
var request = $.ajax({
url: "myurl",
dataType: "json",
type: "GET",
data: serializedData,
});
request.done(function (response, textStatus, jqXHR){
$('#loginMessage').text('GOOD');
});
request.fail(function (jqXHR, textStatus, errorThrown){
$('#loginMessage').text('Some error occured. Please try again');
console.error("The following error occured: ",errorThrown,jqXHR);
});
request.always(function () {
$inputs.prop("disabled", false);
});
// prevent default posting of form
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
我是jquery的新手,在上面的代码中.done块没有执行,firebug控制台显示这条消息: -
GET myurl?userID=aman&password=aman 200 OK 37ms jquery .... min.js(第2行)
发生以下错误:(空字符串) Object { readyState=0, status=0, statusText="error"}
服务器端脚本
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("userID");
String password = request.getParameter("password");
System.out.println("GET");
response.setContentType("application/json");
PrintWriter out = response.getWriter();
Gson gson = new Gson();
if(username.equals("aman") && password.equals("aman")){
out.println(gson.toJson(new Boolean("true")));
}else{
out.println(gson.toJson(new Boolean("false")));
}
out.close();
}
Run Code Online (Sandbox Code Playgroud)
问题可能是您没有发送完整的JSON.我从中输出结果
out.println(gson.toJson(new Boolean("true")));
Run Code Online (Sandbox Code Playgroud)
而你所得到的只是真实的.尝试将其更改为类似的内容.
HashMap<String, Boolean> hm = new HashMap<String, Boolean>();
hm.put("success", true);
out.write(gson.toJson(hm));
Run Code Online (Sandbox Code Playgroud)
运行我得到{"success":true}这是有效的JSON.
| 归档时间: |
|
| 查看次数: |
4093 次 |
| 最近记录: |