2016-02-29 21 views
1

我创建了一个像这样的控制器。如何使用Jquery发送POST请求以弹出mvc应用程序

@Controller 
public class LoginController { 

    @RequestMapping(value = "/hello", method = RequestMethod.GET) 
    @ResponseBody 
    public String hello() { 
     return "hello world"; 
    } 

    @RequestMapping(value = "/login", method = RequestMethod.POST) 
    @ResponseBody 
    public User login(@RequestBody final User user) { 
     return user; 
    } 

} 

当我送了使用bash的请求:

curl -H "Content-Type: application/json" -d '{"username":"xyz"}' http://localhost:8080/mvc/login.do {"id":null,"name":null,"password":null}% 

它运作良好。

但是当我发送这样的请求时,我收到了错误POST http://localhost:8080/mvc/login.do 415 (Unsupported Media Type)

$.ajax({ method: "POST", 
     data: JSON.stringify("{ 
      'id': 'test, 
      'name': 'test1' 
     }"), 
     url:"login.do", 
     timeout: 60000, 
     success: sign_submited, 
     error: ajaxError 
    }); 

回答

3

尝试设置内容类型在您的Ajax请求as-- contentType:"application/json; charset=utf-8",

+0

感谢它的工作原理。 –