2

I'm building a profile page for members on a site where members can change their password. I'm sending the request with ajax, and can successfully change password if I have the input name "password" correct with the users current password.

My problem is that I get a success-response, when I should have gotten an error-response. (Password isn't changed, but I have no way of telling the user there was en error).

My JS is something like this:

$.ajax({
    type: "POST",
    url: "",
    data: dataString,
    success: function(data) {
        console.log("Password successfully changed");
    },
    error: function(data) {
        console.log("Error. Password did not change");
    }
});
  • Haven't actually tested anything yet, but it looks like it should return errors if there is any problems. Have you had a look at UsersController@actionSaveUser? – Fred Carlsen Sep 18 '14 at 9:59
  • Just to clarify: You say "and can successfully change password if I have the input name". That means it works, given that the right password is entered, right? In the second paragraph, do you mean you get an success-response, even if there is an error? See anything in the log? – Fred Carlsen Sep 18 '14 at 10:03
  • @FredCarlsen Correct. It works if the given password is entered. So far, so good. My problem is I don't get error-response. I can't see anything in the log. – Espen Sandberg Sep 18 '14 at 11:21
3

Currently the users/saveUser (and the deprecated users/setPassword) controller action doesn’t support Ajax requests, so there won’t really be an easy way to tell if it worked or not.

If you really want to update user passwords over Ajax, your best bet will be to write a plugin with its own custom controller action, which mimics the password-related stuff in UsersController::actionSaveUser(), but probably calls UsersService::changePassword() rather than saveUser() since you only care about updating the password. And have that controller respond with JSON if it’s an Ajax request, rather than assuming it’s not.

Take a look at EntriesController::actionSaveEntry() for an example of a controller action that does support Ajax.

  • Okay, thanks for the answer. I don't have to use ajax, think I'll stick to regular post. – Espen Sandberg Sep 18 '14 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.