reset password
Author Message
afetiso
Posts: 84
Posted 18:59 Feb 21, 2015 |

What I need to write to run UserController.java using ajax?:

    @RequestMapping(value = "/changePassword.html", method = RequestMethod.POST)
    public @ResponseBody 
    String changePassword( @RequestParam (value = "pass") String pass,
             @RequestParam (value = "userId") Integer userId, ModelMap models ){  }

$.ajax({  
            method: 'post',
            url: 'changePassword.html',
            data: ({ pass: $("#pass").val(), userId: userId.val() }),
            success: function(){}
        });

armandop
Posts: 32
Posted 19:04 Feb 21, 2015 |

Pop open your developer counsole (if you're using Chrome) and check out any errors your ajax call created.

jonathankroening
Posts: 39
Posted 19:05 Feb 21, 2015 |

I think "method" should be "type" ??

type: 'POST'

armandop
Posts: 32
Posted 19:07 Feb 21, 2015 |

Theres actually a few more things wrong with the JS.  I really do suggest opening up the Browsers developer console to debug.  It will stream line your debugging.

jonathankroening
Posts: 39
Posted 19:12 Feb 21, 2015 |

Yeah probably a good call to debug in browser.

But you can at least also get rid of the parentheses around the data value. You only need the braces.

afetiso
Posts: 84
Posted 19:34 Feb 21, 2015 |
jonathankroening wrote:

Yeah probably a good call to debug in browser.

But you can at least also get rid of the parentheses around the data value. You only need the braces.

yes it should be type. But it doesn't change anithing.

the jsp with script jquery function just doesn't call ajax from UserController

I mean i trying call:    

@RequestMapping(value = "/checkpointAdd.html", method = RequestMethod.POST)
    public @ResponseBody String checkpointAdd (@RequestParam(value = "checkPList") List<Integer> checkPList, 
            @RequestParam(value = "userId") Integer userId,
            Principal principal, ModelMap models )

from UserController using ajax from jquery function on button click. But ajax doesn't understend that it should be /checkpointAdd.html request

Last edited by afetiso at 19:37 Feb 21, 2015.
armandop
Posts: 32
Posted 19:43 Feb 21, 2015 |

What does the developr console say?   Start by determining if your ajax call is actually making the call to the backend.  If its not, then no matter what you do to the Java code this piece wont work.  Isolate the issues through each layer of the application.  

 

 

afetiso
Posts: 84
Posted 19:44 Feb 21, 2015 |
armandop wrote:

Theres actually a few more things wrong with the JS.  I really do suggest opening up the Browsers developer console to debug.  It will stream line your debugging.

$(function(){
    $("#save").click(function(){      
        $.ajax({
            type: "POST",
            url: "checkpointAdd.html",
            data: ({ checkPList: selected, userId: userId }),
            success: function(){}
        });    
    });

This should lunch the checkpointAdd.html method in UserController.java with should save new checked checkpoints.

but it doesn't lunch it. I think ajax should be different, or I need to add something to ajax work correct?

afetiso
Posts: 84
Posted 19:45 Feb 21, 2015 |
armandop wrote:

What does the developr console say?   Start by determining if your ajax call is actually making the call to the backend.  If its not, then no matter what you do to the Java code this piece wont work.  Isolate the issues through each layer of the application.  

 

 

no ajax doesn't make a call to the backend

armandop
Posts: 32
Posted 19:49 Feb 21, 2015 |

Ok great. we're getting smewhere.  Baby steps.   Now, why isnt it making the call to the backend?  To figure that out pop open the developer console.  If you're using Chrome, Click on the drawer looking icon on the top right hand, then click n "More Tools", then click on Developer Tools.  When it open up click on Console.  Make sure that you ope this up while you're on your page giving you issues.   

Click on the button again and you should see a few error messages.  Im 10000% sure there are JS error already present.  

afetiso
Posts: 84
Posted 19:56 Feb 21, 2015 |
armandop wrote:

Ok great. we're getting smewhere.  Baby steps.   Now, why isnt it making the call to the backend?  To figure that out pop open the developer console.  If you're using Chrome, Click on the drawer looking icon on the top right hand, then click n "More Tools", then click on Developer Tools.  When it open up click on Console.  Make sure that you ope this up while you're on your page giving you issues.   

Click on the button again and you should see a few error messages.  Im 10000% sure there are JS error already present.  

gives this

Attachments:
armandop
Posts: 32
Posted 19:58 Feb 21, 2015 |

OK awesome.  So before I say anything.  What do you think that is saying?  

PS.  I know im making this harder than it is but trust me in the long run this short headache will save you tons of time.

afetiso
Posts: 84
Posted 20:01 Feb 21, 2015 |
armandop wrote:

OK awesome.  So before I say anything.  What do you think that is saying?  

PS.  I know im making this harder than it is but trust me in the long run this short headache will save you tons of time.

It is ok, thank you. I think main problem that function is anonymus

But I don't know what is wrong

I am changing the password now, because I know that UserController works for it but I was using form:form before, and now I am trying do the same using ajax. so url is correct:

    @RequestMapping(value = "/changePassword.html", method = RequestMethod.POST)
    public @ResponseBody 
    String changePassword( @RequestParam (value = "pass") String pass,
             @RequestParam (value = "userId") Integer userId, ModelMap models )
    {    
        blah blah blah save user with new password
        return "redirect:/planView.html";        
    }

 

and jquery function: 

$(function(){

    $("#save").click(function(){
        $.ajax({  
            method: 'post',
            url: 'changePassword.html',
            data: ({ pass: $("#pass").val(), userId: $("#userId").val() }),
            success: function(){}
        });
    });

});

Last edited by afetiso at 20:08 Feb 21, 2015.
armandop
Posts: 32
Posted 20:07 Feb 21, 2015 |

Humm.  good try.   So lets first check what the console is telling me.  The console is telling me that something made a HTTP POST Request to http://localhost:8080/gefp/changePassword.html  and your server said, "hey dude something is wrong with this request.  I HEAR YOU...but something is wrong"  so it gave you a 400 error (Note the 400 error and NOT 404).     So that tells me that ajax IS making the call to the backend BUT your backend is coughing up a lung and breaking.  

OK, now lets turn our attention to the backend.  Your error logs, do they say anything when you make the call?

Also for fun , when you click on the "network" tab and then click on the POST request can you show the headers to me? I want to see that you are indeed sending POST data over to the backend.  

Last edited by armandop at 20:08 Feb 21, 2015.
afetiso
Posts: 84
Posted 20:10 Feb 21, 2015 |

This is post

Or my gode works thank you

Attachments:
Last edited by afetiso at 20:14 Feb 21, 2015.
armandop
Posts: 32
Posted 20:12 Feb 21, 2015 |

Click on the red one, it should expand the window to the right.  Then click on "Request"  That will give you a print out of all your sending to the backend.  

Also check your error logs for Tomcat, the issue shoud be there.  

 

 

spanfer
Posts: 25
Posted 21:04 Feb 21, 2015 |

Also try to type the URL on the browser directly and see if it is calling the controller.

Comment everything in the controller, and type in some logs or System.println() and see if your controller is called correctly.

 

Also, just wanted to make sure we need to implement User Check/Uncheck using Ajax/jQuery, not sure if we need to implement Change Password and Major using Ajax as well.

cysun
Posts: 2935
Posted 09:55 Feb 22, 2015 |

"data" in the ajax call is an object, so it should be "data: {...}", not "data: ({...})".