Ajax on Success消息

时间:2016-04-07 00:58:45

标签: javascript ajax post

我没有太多使用ajax的经验我想使用.post和in和if条件让用户知道它是否成功。

这是我的.post代码:

$.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") ;

            }) ;

普通的ajax实际上运行良好,但我仍然无法添加失败案例:

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

          }) ;

有人可以帮助我,我觉得这很简单,我做错了。

2 个答案:

答案 0 :(得分:2)

error分配给$.ajax()来电中的某个功能。

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

答案 1 :(得分:1)

jQuery Docs .post()简写:

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

所以实现这样的成功函数:

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

至于在.post()上收到错误..您需要链接失败功能(来自文档)......

// 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" );
});