Does Vue.JS work with AJAX HTTP calls?

I am trying to do the following from my HTML:

var vm = new Vue({
      el: '#loginContent',
      data: {
        main_message: 'Login',
        isLoggedIn: false,
        loginError: '',
        loginButton:'Login'
      },
      methods: {
        onLogin: function() {
          //this.$set(loginSubmit, 'Logging In...');
          var data = {
            email: $('#email').val(),
            password: $('#password').val(),
          };
          $.ajax({
            url: '/api/login',
            data: data,
            method: 'POST'
          }).then(function (response) {
            if(response.error) {
              console.err("There was an error " + response.error);
              this.loginError = 'Error';
            } else {
              //$('#loginBlock').attr("hidden",true);
              console.log(response.user);
              if(response.user) {
                this.isLoggedIn = true;
              } else {
                this.loginError = 'User not found';
              }
            }
          }).catch(function (err) {
            console.error(err);
          });
        }
      }
    });

Basically, the user presses the login button, the onLogin method is called, which sends a message to my API. The message is working fine and I will return the answer in the promise .then ().

But trying to do things like this.isLoggedIn = true;, does not update my DOM with what I expect from HTML when a user logs in.

Could it be that I'm in some kind of background thread (sorry, mobile developer is here) when I get the answer in the promise and he cannot find the instance of "vm"?

thank

+1
source share
3 answers

, , this , $.ajax, - :

  methods: {
    onLogin: function() {
      //this.$set(loginSubmit, 'Logging In...');
      var data = {
        email: $('#email').val(),
        password: $('#password').val(),
      };
      var that = this
      $.ajax({
        url: '/api/login',
        data: data,
        method: 'POST'
      }).then(function (response) {
        if(response.error) {
          console.err("There was an error " + response.error);
          that.loginError = 'Error';
        } else {
          //$('#loginBlock').attr("hidden",true);
          console.log(response.user);
          if(response.user) {
            that.isLoggedIn = true;
          } else {
            that.loginError = 'User not found';
          }
        }
      }).catch(function (err) {
        console.error(err);
      });
    }
  }
+3

ES6 Arrow, '= > '. . :

      $.ajax({
        url: '/api/login',
        data: data,
        method: 'POST'
      }).then((response) => {
        if(response.error) {
          console.err("There was an error " + response.error);
          this.loginError = 'Error';
        } else {
          //$('#loginBlock').attr("hidden",true);
          console.log(response.user);
          if(response.user) {
            this.isLoggedIn = true;
          } else {
            this.loginError = 'User not found';
          }
        }
      }).catch(function (err) {
        console.error(err);
      });
0

Perhaps you should take a look at axios . I used $ .ajax and got it working, but found axioms and preferred axioms over the ajax library.

0
source

Source: https://habr.com/ru/post/1664239/


All Articles