Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jquery将参数传递给控制器_Java_Jquery - Fatal编程技术网

Java 使用jquery将参数传递给控制器

Java 使用jquery将参数传递给控制器,java,jquery,Java,Jquery,我已经创建了一个视图和控制器,我正在尝试使用jquery将表单参数传递给控制器 $.ajax({ type: "POST", url: "add.htm", datatype: "json", data: "name=" + name + "&password=" + password + "&gender=" + gender + "&aboutyou=" + aboutyou, success: function

我已经创建了一个视图和控制器,我正在尝试使用jquery将表单参数传递给控制器

  $.ajax({  
    type: "POST",  
    url: "add.htm", 
    datatype: "json",
    data: "name=" + name + "&password=" + password + "&gender=" + gender + "&aboutyou=" + aboutyou, 
    success: function(response){
        alert(response.name);
      // we have the response 
      if(response.status == "SUCCESS"){
          $('#info').html("User has been added to the list successfully");
          /* $('#name').val('');
          $('#education').val(''); */
      }else{
          $('#info').html("Sorry, there is some thing wrong with the data provided.");
      }       
    },  
    error: function(e){  
      alert('Error: ' + e);  
    }  
  });  
}  
这是我的控制器

      public ModelAndView add(HttpServletRequest request,
        HttpServletResponse response, employee employee) throws Exception {
    List list=new ArrayList();
    employeedao.saveUser(employee);
    return new ModelAndView("userform");
}

我认为您希望获得控制器中的值(从问题标题中),如果这是要求,那么使用
request.getParameter(“parametername”)

例如,要获取名称,请执行以下操作

request.getParameter("name"); 
您还需要更改传递参数的格式,如

数据:{parametername:parametervalue}
作为JSON


示例
数据:{name:name….}
替换下一行:-

 data: "name=" + name + "&password=" + password + "&gender=" + gender + "&aboutyou=" + aboutyou,
 data: { name:name,password:password,gender:gender,aboutyou:aboutyou}
使用以下行:-

 data: "name=" + name + "&password=" + password + "&gender=" + gender + "&aboutyou=" + aboutyou,
 data: { name:name,password:password,gender:gender,aboutyou:aboutyou}

您的数据格式不正确

它的格式是

{ name: "John", time: "2pm" }

您发送的数据与普通数据类似,但在jquery中,格式不同,如下所述

$.ajax({  
type: "POST",  
url: "add.htm", 
datatype: "json",
data: { "name":name,"password":password,"gender":gender,"aboutyou":aboutyou},
success: function(response){
    alert(response.name);
  // we have the response 
  if(response.status == "SUCCESS"){
      $('#info').html("User has been added to the list successfully");
      /* $('#name').val('');
      $('#education').val(''); */
  }else{
      $('#info').html("Sorry, there is some thing wrong with the data provided.");
  }       
},  
error: function(e){  
  alert('Error: ' + e);  
}  
  });  
}  
这是发送数据的格式。 谢谢
纳维

很好的尝试。。。但问题是什么?我的表单值没有从jquery传递到控制器…名称、密码、性别和关于您的参数没有从jquery传递到控制器与OP的问题I无关thinks@user2310289从标题来看,我认为这和json格式也是错误的,谢谢..但是我的警报(response.name)不起作用…未定义..很好地发现jsonformat@mohittomar您是否更改为JSON格式?