Simone Venturas Simone Venturas - 2 months ago 12
jQuery Question

Ajax throwing fail function... But it does what it should do

I've got a weird behavior in my PHP/jQuery project.

When I call the Ajax request, it works as intended, but instead of calling the 'done' function, it calls the 'fail' one.

This is my code at the moment.

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

});
});


And this is my 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");
}

?>


The message warn I get on the console is..

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


But the table 'users', the config file and the admin user are created however.

How can I fix it?

I will provide the code of the functions on request.

Answer

I think that the error that you see is that your code submits 2 times.. try to add this line:

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

this should work ok now..