Javascript Don';我不知道为什么AJAX表单数据向MVC控制器发送null

Javascript Don';我不知道为什么AJAX表单数据向MVC控制器发送null,javascript,jquery,ajax,spring,Javascript,Jquery,Ajax,Spring,我使用AJAX发送表单数据,我的springmvc控制器得到null值,但是如果我使用,它将正常工作 这是我的ajax调用: $.ajax({ type: "post", url:"http://localhost:8080/fivenet/mobile/api/login", crossDomain: true, data: JSON.stringify(formData), dataType: 'json', contentType: 'tex

我使用AJAX发送表单数据,我的springmvc控制器得到
null
值,但是如果我使用,它将正常工作

这是我的ajax调用:

$.ajax({
    type: "post",
    url:"http://localhost:8080/fivenet/mobile/api/login",
    crossDomain: true,
    data: JSON.stringify(formData),
    dataType: 'json',
    contentType: 'text/html;charset=UTF-8',
    //contentType: 'application/json;charset=utf-8',
    beforeSend: function(xhr){
    },
    success: function(msg){
        console.log('sucess');
    },
 error: function(msg){}
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "create-account", method = RequestMethod.POST)
public @ResponseBody RespondMessage createAccount(
        @RequestParam(value = "data", required = true) String dataString,
        HttpServletRequest request) throws Exception {

    RespondMessage responsed = new RespondMessage();
    headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    JSONObject data = new JSONObject(dataString);
 return responsed;
}
这是我的控制器:

$.ajax({
    type: "post",
    url:"http://localhost:8080/fivenet/mobile/api/login",
    crossDomain: true,
    data: JSON.stringify(formData),
    dataType: 'json',
    contentType: 'text/html;charset=UTF-8',
    //contentType: 'application/json;charset=utf-8',
    beforeSend: function(xhr){
    },
    success: function(msg){
        console.log('sucess');
    },
 error: function(msg){}
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "create-account", method = RequestMethod.POST)
public @ResponseBody RespondMessage createAccount(
        @RequestParam(value = "data", required = true) String dataString,
        HttpServletRequest request) throws Exception {

    RespondMessage responsed = new RespondMessage();
    headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    JSONObject data = new JSONObject(dataString);
 return responsed;
}
请尝试以下代码:

var formData = {"username" : "username", "password" : "password"};

$.ajax({
    type: "POST",
    url: "http://localhost:8080/fivenet/mobile/api/login",
    data: JSON.stringify(formData),
    dataType: 'json',
    contentType: 'application/json;charset=utf-8',
    beforeSend: function(xhr){
    },
    success: function(data){
        alert(data)
    },
    failure: function(errMsg) {
        alert(errMsg);
    },
    error: function(errMsg){
         alert(errMsg);
    }

我终于解决了这个问题。这就是我提供数据的方式

$.ajax({
    type: 'POST',
    url: "http://localhost:8080/fivenet/mobile/api/login",
    crossDomain: true,
    data: {"data": JSON.stringify(formData)},*** This is where the error is
    dataType: 'json',
    beforeSend: function(xhr){

    },
    success: function(msg){
        console.log('sucess');
    },
 error: function(msg){}

感谢大家的关注

JavaScript控制台中是否发生错误?你能准确地发布你在POSTman中使用的参数吗?是的,它返回500,当我在调试模式下运行spring时,“String dataString”返回null。该参数类似于{“username”:xxx@gmail.com,“密码”:“12345”}您如何填写
formData
?初始化它的代码在哪里?当我尝试@Chandra Kumar方法时,我得到了这样一个结果:XMLHttpRequest无法加载。对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。因此不允许访问源“null”。请更新您的登录web服务url:
http://localhost:8080/fivenet/mobile/api/login
**我有相同的错误**POST 500()请求URL:请求方法:POST状态代码:500远程地址:[::1]:8080推荐人策略:降级时无推荐人