huanming
huanming
  • 发布:2016-03-21 15:45
  • 更新:2017-05-04 22:25
  • 阅读:8399

mui.ajax请求失败的问题

分类:MUI
  1. ajax接口调用的如下:
    
    mui.ajax('http://192.188.140.92:8080/sfview/login.action', {  
                        data:d,  
                        dataType:'json',   
                        type:'post',   
                        timeout:10000,   
                        success: function(data){  
                        },  
                        error:function(xhr, type, errorThrown){  
                    });  
2. login.action的服务处理回调采用如下@responseBody指定要求返回值为json数据时,会引起上面调用失败:

@ResponseBody
@RequestMapping(value="login.action", method=RequestMethod.POST)
public Msg<user> Login(String username, String password, HttpServletResponse response)
{
Msg<User> msg = new Msg<User>();

    return msg;   

}

注意:在这样的模式下,使用web模式的http请求服务端的jsp页面,同样采用ajax请求login服务,结果正常。  

3. 如服务端该为如下方式则ajax调用成功:

@RequestMapping(value="login.action", method=RequestMethod.POST)
public void Login(String username, String password, HttpServletResponse response)
{
Msg<User> msg = new Msg<User>();
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}

    JSONObject json = JSONObject.fromObject(msg);  
out.println(json);  

}

2016-03-21 15:45 8 条评论 负责人:无 分享
已邀请:
y***@foxmail.com

y***@foxmail.com

针对你第一种的方式 追加produces="application/json;charset=UTF-8"就可以啦~

该问题目前已经被锁定, 无法添加新回复