温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jquery中$.post方法不执行怎么处理

发布时间:2020-11-26 09:55:37 来源:亿速云 阅读:503 作者:栢白 栏目:web开发

本篇文章和大家了解一下jquery中$.post方法不执行怎么处理。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

jquery $.post方法不执行的解决方法:设置返回的数据为json数据,代码为【out.print(JSONObject.fromObject(result));】。

jquery中$.post方法不执行怎么处理

  • 该方法适用于所有品牌电脑

jquery $.post方法不执行的解决方法:

关于JQuery的post回调函数不执行问题

前端的post回调函数就是不执行, 设置完断点debug还是直接跳过不执行

错误演示 :

后台代码

@RequestMapping(value = "login")
public void CheckUserAccount(@RequestParam("account")String account, HttpServletResponse response) throws IOException{
JSONObject json_account = JSONObject.fromObject(account);
int username = (Integer)json_account.get("username");
String password = json_account.getString("password");
logger.info("username = " + username + " password = " + password);
int status;
status = checkUserAccountService.IsCorrect(username, password);
Map<String, Object> result = new HashMap<String, Object>();
Map<String, String> info_json = new HashMap<String, String>();
if(status == 1){
info_json.put("status", "success");
}else if(status == 0){
info_json.put("status", "errorpassword");
}else{
info_json.put("status", "noexitaccount");
}
result.put("info", info_json);
JSONObject json_object = JSONObject.fromObject(result);
response.setContentType("text/json; charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.print(result);
out.flush();
out.close();
logger.info("验证结果是" + ((Map<String, String>)(result.get("info"))).get("status"));

重点的一句是:

response.setContentType("text/json; charset=utf-8");

此处设置返回的数据为json数据, 但是out.print(result);这里输出的是一个Map, so 前端识别不出任何数据, 结果回调函数直接跳过不执行(因为回调函数只执行json数据)

正确示范 :

改成: out.print(JSONObject.fromObject(result));

就可以了。

以上就是jquery中$.post方法不执行怎么处理的简略介绍,当然详细使用上面的不同还得要大家自己使用过才领会。如果想了解更多,欢迎关注亿速云行业资讯频道哦!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI