6

I am trying to verify username and other fields while creating a change password page.The problem is AJAX call in Jquery script is not hitting my controller.i tried giving hard coded path also in url field of the ajax request. Below is my Script

this checkUname function is triggering on onblur event from one of the input field.

 <script type="text/javascript">
        function checkUname() 
            {
                // get the form values

                var uName = $('#username').val();
                var secQues = $('#secQues').val();
                var secAns = $('#secAns').val();

                var dataObject = JSON.stringify({
                                                    'uName'  : uName,
                                                    'secQues': secQues,
                                                    'secAns' : secAns
                                                });
             $.ajax({
                    url:"validateCredentials.do" ,
                    type: "POST" ,
                    data: dataObject ,
                    contentType: "application/json; charset=utf-8" ,
                    dataType : 'json' ,                 
                    success: function(response)
                                                {
                                                 alert(response);                           
                                                } ,
                    error: function()
                                        {
                                        alert('Error fetching record.... Sorry..');
                                        }
                    }); 
         }
        </script>

This is my MVC controller

    @Controller
public class ArsController 
{
    @RequestMapping(value="validateCredentials.do", method = RequestMethod.POST)
    public String changePass(@RequestParam("uName") String uName ,@RequestParam("secQues")String secQues,
                                   @RequestParam("secAns") String secAns)
    {
        System.out.println("AJAX request");
        Users dummyUSer = null;
        String msg = null;

                try 
                {
                    dummyUSer = servObj.findUser(uName);
                } 
                catch (ArsException e) 
                {
                    System.out.println("error occurred while validating user data during password change");
                    e.printStackTrace();
                }

                if(dummyUSer == null)
                {
                    msg = "No user exists with this username";
                }

                else
                {
                    if(!secQues.equals(dummyUSer.getSecQues()))
                    {
                        msg = "Security question is not correct";
                    }
                    else
                    {
                        if(!secAns.equals(dummyUSer.getSecAns()))
                        {
                            msg = "Security answer does not match";
                        }
                    }
                }

        return msg;
    }
Sourabh
  • 119
  • 1
  • 9
  • I don't have idea about Java code but AJAX request has `type: "POST"` whereas it should be `method: "POST"` – Deepansh Sachdeva Sep 15 '17 at 18:17
  • I made changes as you suggested , but now em getting " INFO: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level. java.lang.IllegalArgumentException: Invalid character found in the request target " – Sourabh Sep 15 '17 at 18:23
  • Maybe you could use this [solution](https://stackoverflow.com/questions/18819180/tomcat-7-0-43-info-error-parsing-http-request-header) but I'm not sure whether it'll help as I don't know much about Java. Also, your AJAX request is working fine now, you have to debug issue from backend / server. – Deepansh Sachdeva Sep 15 '17 at 18:26
  • What version of jquery? `type` and `method` are synonymous in later versions but if you are earlier than 1.9 you have to use `type`. If you are using `method` before 1.9 you might expect this error since it might be trying to send password characters via `get` which likely contain some illegal characters for a URL. – stephen.vakil Sep 15 '17 at 18:31
  • i am using jquery-1.8.3.min.js , i will change it back to type ? – Sourabh Sep 15 '17 at 18:35
  • Yes. Also try adding `processData: false` to your ajax call and see if that makes a difference. – stephen.vakil Sep 15 '17 at 18:43
  • Try to replace `@Controller` by `@RestController`. Also, `RequestParam` could be invalid because they are in `@RequestBody`, like map or other JavaDTO – Artur Czopek Sep 15 '17 at 19:51
  • Are you getting any console errors in your browser? – twoleggedhorse Sep 15 '17 at 20:21
  • Also try typing the entire url in your url field just to test it's not a routing issue. http:// etc – twoleggedhorse Sep 15 '17 at 20:22
  • Also utf-8 should really be UTF-8 but the whole charset bit isn't required, you just need application/json – twoleggedhorse Sep 15 '17 at 20:27
  • Lastly you may need to make CORS enabled on your server side – twoleggedhorse Sep 15 '17 at 20:29
  • And if you are doing cross origin requests then your datatype should be jsonp – twoleggedhorse Sep 15 '17 at 20:30
  • 1
    Instead of using RequestParam in controller, you should use String. Because when we are posting JSON data it will not come as individual parameters in Request, instead it will be received as String in your controller. Once you get the String convert it to JSON object and then get your values accordingly. – Nishesh Pratap Singh Sep 16 '17 at 04:01
  • hi nishesh , i tried your suggestion and it worked for me , thank you so much – Sourabh Sep 16 '17 at 07:12
  • @nisheshpratap Please post your comments as an answer – twoleggedhorse Sep 16 '17 at 13:46
  • It seems like your url is wrong. Try the same url in any api testing tool like postman – Beingnin Sep 16 '17 at 16:18
  • @twoleggedhorse, it's done. – Nishesh Pratap Singh Sep 17 '17 at 06:57

2 Answers2

2

Instead of using RequestParam in controller, you should use String. Because when we are posting JSON data it will not come as individual parameters in Request, instead it will be received as String in your controller. Once you get the String convert it to JSON object and then get your values accordingly.

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20
0

try remove content-type and data-type. You are not sending a json that should be parsed in a object, you are sending controller's parameters. The way to do that is using an object in the Ajax 's data (as you did) but without the content-type or data-type that saying "I'm sending one json parameter"

frisk
  • 75
  • 1
  • 10