0

Trying to call Spring Controller from jsp code. We are getting a error message Error object [Object]. What's missing ?

JSP code

    <script type="text/javascript">
function doAjaxPost() {  
      // get the form values  
      var firstName = $('#firstName').val();
      var lastName = $('#lastName').val();

      alert(firstName + " and " + lastName);

      $.ajax({  
        type: "GET",
        url: "/DriverController/AddUser",  
        data: "firstName=" + firstName + "&lastName=" + lastName,  

        success: function(response){  
            alert(data);
          // we have the aresponse  
          $('#info').html(response);
         $('#firstName').val('');
          $('#lastName').val('');
        }
      ,  
        error: function(e){  
          alert('Error: ' + e);  
        }  
      });  
    }  
</script>

Controller Code

package net.codejava.spring;
public class DriverController {
private List<DriverPojoWrapper> userList = new ArrayList<DriverPojoWrapper>(); 

    @RequestMapping(value="/AddUser",method=RequestMethod.GET)
    public String showForm(){
        System.out.println("Calling DriverController showform /AddUser GET");
        return "AddUser";
    }

    @RequestMapping(value="/AddUser",method=RequestMethod.POST)
    public @ResponseBody String addUser(@ModelAttribute(value="user") DriverPojoWrapper driver, BindingResult result ){
        String returnText;
        if(!result.hasErrors()){
            userList.add(driver);
            returnText = "User has been added to the list. Total number of users are " + userList.size();
        }else{
            returnText = "Sorry, an error has occur. User has not been added to list.";
        }
        return returnText;
    }

    @RequestMapping(value="/ShowUsers.htm")
    public String showUsers(ModelMap model){
        model.addAttribute("Users", userList);
        return "ShowUsers";
    }

The servet-controller xml has

<context:component-scan base-package="net.codejava.spring" />
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • Your data should be `data : { firstName : firstName, lastName : lastName}`, Refer [jQuery ajax](http://api.jquery.com/jquery.ajax/) – Vinoth Krishnan May 02 '16 at 10:13
  • See [here](http://stackoverflow.com/questions/6792878/jquery-ajax-error-function#14563181), how to know the error from ajax. – Vinoth Krishnan May 02 '16 at 10:21

0 Answers0