简体   繁体   中英

Object error-Ajax Call via Spring MVC

Screen Shot of an ERROR I am trying to insert value using AJAX call via Spring MVC controller. But its threwing the Object Erorr on click of button. Please can anyone help me regarding this issue.

CODE: Ajax Code:

  <script type="text/javascript">
    //     $(document).ready(function() {
    function doAjaxPost() {
        //           // get the form values     
        $.ajax({
            type : "POST",
            async: "false",
//          url : "${pageContext.request.contextPath}/leadstatus_creation",
            url :'/ajax/leadstatus_creation',
            data: $('#frm-createlead-status').serialize(),
            success : function(data) {
            if(data != null && data !='')
                {
                    $('#txtleadname').val(data); 
                }               
            },  
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    }
    //           });
</script>

LeadController.java

/**
     * Method use for View lead status get method.
     * @param map
     * @return
     * @throws Exception 
     */

@RequestMapping(value="/ajax/leadstatus_creation",method=RequestMethod.POST)
public @ResponseBody String createleadstatus(BindingResult result,HttpSession session,HttpServletRequest request,HttpServletResponse response) throws Exception{
    String resultStr=leadDao.createLeadStatus(null);        
    try 
    {   
        if(session !=null)
        {               
            String leadstatus = request.getParameter("txtleadname");
            Map<Integer,Object>obj=null;
            obj=new HashMap<Integer , Object>();
            obj.put(1, leadstatus);
            obj.put(2, 1);
            if(leadDao.createLeadStatus(obj) != null)
            {
                resultStr = "true";

            }
            else
            {
                resultStr = "false";

            }

            resultStr = JSONValue.toJSONString(resultStr);
        }
        response.setContentType("application/json");
        response.getWriter().write(resultStr.toString());           

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    return resultStr;
}
}

Form Code:

Add the id to button that your are using to post the data in spring controller

<a id="frm-createlead-status" > POST </a>

then you can use the 'click' event to execute the function when your page is loaded,

<script type="text/javascript">

$(document).ready(function(){    
    $('#frm-createlead-status').on('click', function(event){


        $.ajax({
            url :'/ajax/leadstatus_creation',
            type: 'POST',
            data: $(#frm-createlead-status).serialize(),
            success: function(data){
                if(data != null && data !='')
                {

                  $('#txtleadname').val(data); 
                } 
            },               
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }

        });

    });
});

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM