Ajax抛出失败函数......但是它做了它应该做的事情

时间:2016-09-26 07:08:54

标签: php jquery ajax post

我的PHP / jQuery项目中有一个奇怪的行为。

当我调用Ajax请求时,它按预期工作,但不是调用'done'函数,而是调用'fail'函数。

这是我目前的代码。

$("#form").submit(function(){

    var _this = $(this);
    var values = {};
    $.each($('#form').serializeArray(), function(i, field) {
        values[field.name] = field.value;
    });
  //  _this.button('loading');
    $.ajax({
      method: "POST",
      url: "include/ajax/install_ajax.php",
      data: values,
      dataType: 'json'
    })
      .done(function( msg ) {
        console.log( "Done: " + msg );
      })
      .fail(function(xhr, textStatus, errorThrown){
            console.warn('Xhr: ' + xhr.responseText);
            console.warn('textStatus: ' + textStatus);
            console.warn('errorThrown: ' + errorThrown);

      }); 
});

这是我的install_ajax.php

<?php

// Chiama le funzioni del sito
require('../functions.php');

// Controlla se è una richiesta AJAX.
is_ajax();


    try {

    $args['db_host'] = $_POST['db_host'];
    $args['db_username'] = $_POST['db_username'];
    $args['db_password'] = $_POST['db_password'];
    $args['db_name'] = $_POST['db_name'];
    $args['admin_email'] = $_POST['admin_email'];
    $args['admin_username'] = $_POST['admin_username'];
    $args['admin_password'] = $_POST['admin_password'];

    $structure = 
    "-- --------------------------------------------------------

    --
    -- Struttura della tabella `users`
    --

    CREATE TABLE IF NOT EXISTS `users` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `username` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `email` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `name` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `surname` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `password` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `admin` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
      `validated` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1 ;";

    installDB($args['db_host'],$args['db_username'],$args['db_password'],$args['db_name'],$structure);

    createAdmin($args['db_host'],$args['db_username'],$args['db_password'],$args['db_name'],$args['admin_email'], $args['admin_username'], $args['admin_password']);

    createConfig($args['db_name'],$args['db_username'],$args['db_password'],$args['db_host']);
    $return = array();

    $return['success'] = true;

    return_json($return);

} catch (Exception $e) {
   error_log($e->getMessage() . "\n", 3, "/var/tmp/error.log");
}

?>

我在控制台上发出的消息警告是..

core.js:31 Xhr: undefined
core.js:32 textStatus: error
core.js:33 errorThrown: undefined

但是创建了表'users',配置文件和admin用户。

我该如何解决?

我会根据要求提供功能代码。

4 个答案:

答案 0 :(得分:1)

我认为您看到的错误是您的代码提交了2次...尝试添加此行:

$("#form").submit(function(event){
    event.preventDefault();  //prevent form from submitting, and only do ajax call
    var _this = $(this);
    // etc code..
}

这应该可以正常工作..

答案 1 :(得分:0)

试试这个

$("#form").submit(function(){
    var _this = $(this);
    var values = {};
    $.each($('#form').serializeArray(), function(i, field) {
        values[field.name] = field.value;
    });
  //  _this.button('loading');
    $.ajax({
      method: "POST",
      url: "include/ajax/install_ajax.php",
      data: values,
      dataType: 'json',
      success: function(response){
        alert(response);
      },
      error: function(error){
        alert(error);
      }
    });
});

答案 2 :(得分:0)

尝试使用

返回输出
return json_encode($return);

而不是

return_json($return);

答案 3 :(得分:0)

    In PHP
    in try 
    statement add echo json_encode(array('success'=>'1')); die;

and in Catch statement add echo json_encode(array('failed'=>'1')); die;


    replace your jquery submit code with below

    $("#form").submit(function(submitEvent){
        submitEvent.preventDefault();
        var _this = $(this);    
      //  _this.button('loading');
        $.ajax({
          method: "POST",
          url: "include/ajax/install_ajax.php",
          data: _this.serializeArray(),
          dataType: 'JSON',
          success: function(response){
            console.log(response);
          },
          error: function(error){
            console.warn(error);
          }
        });      
    });

and check browser console