display json data coming from AJAX request

0

I do a post via ajax, and my php returns the data in json, but I can not get that data back.

PHP:

    if (empty($_POST['unidade'])) { 
    $errors['erro'] = 'unidade cannot be blank';
}
if (!empty($errors)) { //If errors in validation
    $form_data['success'] = false;
    $form_data['erros']  = $errors;
}
else { //If not, process the form, and return true on success
    $form_data['success'] = true;
    $form_data['enviado'] = 'Cadastrado';
}

//Return the data back to form.php
echo json_encode($form_data);

Ajax:

request = $.ajax({
                url: "db/enviarexercicio.php",
                type: "post",
                data: formdata
            });

            // Callback handler that will be called on success
            request.done(function (response, textStatus, jqXHR){
                // Log a message to the console
                console.log("Hooray, it worked!");
                console.log(textStatus);
                console.log(response);
                var data = JSON.parse(response.responseText);

                if (!data['success']) { //If fails
                                alert(data['erros']);
                                //$('.throw_error').fadeIn(1000).html(response.errors.name); //Throw relevant error

                        }
                        else {
                            alert(data['enviado']);
                                //$('#success').fadeIn(1000).append('<p>' + response.posted + '</p>'); //If successful, than throw a success message
                            }


            });

            // Callback handler that will be called on failure
            request.fail(function (jqXHR, textStatus, errorThrown){
                // Log the error to the console
                console.error(
                    "The following error occurred: "+
                    textStatus, errorThrown
                );
            });
    
asked by anonymous 17.01.2018 / 17:14

0 answers