vendredi 24 juillet 2015

Using reCAPTCHA with Ajax in Spring MVC

I am trying to use google reCAPTCHA service with spring MVC.But I am getting this error on console:

http://localhost:8080/spapp/ajaxtest.htm?name=frfr&surname=frfr&phone=edede…nGi3CCrD9GprYZDn8VHc-pN--SK-u_xoRKOrnRc_ISQh&recaptcha_response_field=1404 Failed to load resource: the server responded with a status of 400 (Bad Request)

Here is My AJAX JavaScript Codes:

$(document).on("click","button#save",function(){ 

                 var name=$("input#name").val();
                 var surname=$("input#surname").val();
                 var phone=$("input#phone").val();
                 var recaptcha_challenge_field=$("#recaptcha_challenge_field").val();
                 var recaptcha_response_field=$("#recaptcha_response_field").val();

                 var error=0;


                 if(name==""){

                     $("label#stateName").html("First Name field must be filled!");

                     error=1; 
                 }
                 if(surname==""){

                     $("label#stateSurName").html("Last Name field must be filled!");

                     error=1; 
                }




                if(error==0) {

                $("div#addResult").html("<img src=\"resources/images/loading.gif\" style=\"max-width:50px;max-height:50px;margin-left:250px;\"/>");

                setTimeout(
                  function() 
                  {

                    $.ajax({
                        url:"ajaxtest.htm",
                        data:{name:name,
                              surname:surname,
                              phone:phone,
                              recaptcha_challenge_field:recaptcha_challenge_field,
                              recaptcha_response_field:recaptcha_response_field},
                        success:function(response){
                            $("div#addResult").html(response);

                        }
                    });

                  }, 2000);

                 }
            });

Here My Controller Method:

@RequestMapping(value = "/ajaxtest", method = RequestMethod.GET) 
    public @ResponseBody String ajaxCRUDPerson( 
           @ModelAttribute Person person, 
           ModelMap model,
           @RequestParam(value="id") String id,
           @RequestParam(value="name") String name,
           @RequestParam(value="surname") String surname,
           @RequestParam(value="phone") String phoneNumber,
           @RequestParam("recaptcha_challenge_field") String challangeField, 
           @RequestParam("recaptcha_response_field") String responseField,
           ServletRequest servletRequest) {


        String remoteAddress = servletRequest.getRemoteAddr();
        ReCaptchaResponse reCaptchaResponse = this.reCaptcha.checkAnswer(remoteAddress,challangeField, responseField);

        String result = "";

        if(reCaptchaResponse.isValid()){

        if(StringUtils.hasText(person.getId())) {
            person.setName(name);
            person.setSurname(surname);
            person.setPhoneNumber(phoneNumber);
            personService.updatePerson(person);
            result+="The person has been updated successfully! "+person.getName();

        } else {
            person.setName(name);
            person.setSurname(surname);
            person.setPhoneNumber(phoneNumber);
            personService.addPerson(person);
            result+="The person has been inserted successfully!";   

        }

        model.addAttribute("personList", personService.listPerson());

        return result;

        }else{

            return "Invalid answer!";

        }
    }

I couldnt understand where is wrong.

How Can I fix this error!

Thanks

Aucun commentaire:

Enregistrer un commentaire