移动ajax代码不起作用

时间:2017-11-08 19:36:54

标签: php jquery ajax

我对标题不好,抱歉。

我有一个工作的ajax代码,我移动到一个单独的函数(' sendForm') - 在此之后它停止工作,重定向到实际的PHP并给出未定义的索引警告。

我认为代码是最好的解释:

function sendForm(_type, _func) {
  $.ajax( {
    url:      "/lib/auth.php",
    type:     "POST",
    data:     { type: _type,
                js: true,
                form: $('#auth_form').serialize()
              },
    success:  _func(_result) /* This should maybe be just '_func'? Either way, same behavior */
  } );
}

function loginEvent() {
  /* This works if not commented

    $.ajax( {
    url:      "/lib/auth.php",
    type:     "POST",
    data:     { type: 'login',
                js: true,
                form: $('#auth_form').serialize()
              },
    success:  function(_result) {
      if (_result == "true")
        window.location = '/index.html';
      else {
        $('h4.header').removeClass('hide');
        $('input[name=password]').val('');
      }
    }
  } );*/

  /* Trying to call the function, with an inline function as argument*/
  sendForm('login', function(_result) {
      if (_result == "true")
        window.location = '/index.html';
      else {
        $('h4.header').removeClass('hide');
        $('input[name=password]').val('');
      }
    }
  );

  return false;
}

当然还有来自/lib/auth.php的警告,它们甚至不应该作为一个页面打开:

Notice: Undefined index: form ...
Notice: Undefined index: type ...
Notice: Undefined index: js ...

我可能会忽视某些事情,所以感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

为了记录评论之外的解决方案,我们确定在表单提交过程中缺少event.preventDefault()电话。