Spring 2.x,ajax无法正常工作

时间:2014-06-05 09:13:03

标签: ajax spring-mvc

我正在尝试为我的spring项目添加ajax功能,但它无法正常工作。下面是我遵循的代码片段。如果出现问题,请告诉我

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
 pageEncoding="ISO-8859-1"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>Insert title here</title>  
<script src="js/jquery-1.10.1.js" type="text/javascript"></script>  

</head>  
<body>  
 <center>  



 <b>Being Java Guys | Registration Form </b>   



  <script>  
   function doAjaxPost() {  


    var name = $('#name').val();  
    var gender = $('#gender').val();  
    var email = $('#email').val();  
    var phone = $('#phone').val();  
    var city = $('#city').val();  

    $.ajax({  
     type : "Get",   
     url : "hello.html",   
     data : "name=" + name + "&gender=" + gender + "&email="  
       + email + "&phone=" + phone + "&city=" + city,  
     success : function(response) {  
      alert(response);   
     },  
     error : function(e) {  
      alert('Error: ' + e);   
     }  
    });  
   }  
  </script>  
  <div id="form">  
   <form method="get">  
    <table>  
     <tr>  
      <td>Name :</td>  
      <td><input type="text" id="name" /></td>  
     </tr>  
     <tr>  
      <td>Gender :</td>  
      <td><input type="radio" id="gender" value="male" /> Male   

       <input type="radio" id="gender" value="female" /> Female</td>  
     </tr>  
     <tr>  
      <td>Email :</td>  
      <td><input type="text" id="email" /></td>  
     </tr>  
     <tr>  
      <td>Phone :</td>  
      <td><input type="text" id="phone" /></td>  
     </tr>  
     <tr>  
      <td>City :</td>  
      <td><select id="city"><option value="noida">Noida</option>  
        <option value="delhi">Delhi</option>  
        <option value="gurgaon">Gurgaon</option>  
        <option value="others">Others</option>  
      </select></td>  
     </tr>  
     <tr>  
      <td> </td>  
      <td><input type="button" value="Save" onclick="doAjaxPost();" />  
      </td>  
     </tr>  
    </table>  
   </form>  
  </div>  
 </center>  
</body>  
</html>  

服务器代码:

 package com.beingjavaguys.controller;  
/** 
 * @author Nagesh Chauhan 
 */  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  

@Controller  
public class Home {  

 @RequestMapping("/hello")  
 public @ResponseBody  
 String hello(@RequestParam(value = "name") String name,  
   @RequestParam(value = "gender") String gender,  
   @RequestParam(value = "email") String email,  
   @RequestParam(value = "phone") String phone,  
   @RequestParam(value = "city") String city) {  
  System.out.println(name);  
  System.out.println(gender);  
  System.out.println(email);  
  System.out.println(phone);  
  System.out.println(city);  

  String str = "{\"user\": { \"name\": \"" + name + "\",\"gender\": \""  
    + gender + "\",\"email\": \"" + email + "\",\"phone\": \""  
    + phone + "\",\"city\": \"" + city + "\"}}";  
  return str;  

 }  
}  

我关注了一个网站,因为这是我第一次这样做,但这没有用到

0 个答案:

没有答案