简体   繁体   中英

Ajax on Success message

I don't have much experience with ajax I want to use .post and in and if condition let the user know it succeed or not.

This is my .post code:

$.post("ajaxRegistering.php",{
                  name: name,
                  lastname: lastname,
                  secondlastname: secondlastname,
                  usernameone: usernameone,
                  email: email,
                  passwordone: passwordone,
                  studentnumberone: studentnumberone,
                  temp: temp
            },
                function(data,status){
                  // alert("Data: " + name + "\nStatus: " + status) ;
                  sweetAlert("Successfully", " created your account.", "success") ;

            }) ;

Normal ajax that actually runs well but I'm still unable to add on failure case:

$.ajax ({
            type: "POST",
            url: "website.php",
            data: data,
            success: function(dataone){
              sweetAlert("Successfully", " rolled in momentoSagrado.", "success") ;
            } 

          }) ;

Can someone help me out I think is something simple that I'm doing wrong.

Assign error to a function in your $.ajax() call.

$.ajax({
    type: "POST",
    url: "website.php",
    data: data,
    success: function(dataone) {
        sweetAlert("Successfully"," rolled in momentoSagrado.","success");
    },
    error: function(xhr, status, errorThrown) {
        // error occured
    }
});

per jQuery Docs .post() is short hand for:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

so implement a success function like this:

$.post( "website.php", function( data ) {
    sweetAlert("Successfully", " created your account.", "success") ;
});

As far as getting the error on .post() .. you'll need to chain the fail function (from docs) ...

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM