Ajax

Ajax.

Overview

Ajax.


<link href="https://laarmwork.arnaudjacques.fr/res/css/button.css" rel="stylesheet">
<script src="https://laarmwork.arnaudjacques.fr/res/js/laarmwork.js"></script>
            

ajax.html


<div class="flex space-evenly">
    <a class="btn success " data-error="false">Send success in Ajax</a>
    <a class="btn danger" data-error="true">Send error in Ajax</a>
    <a class="btn primary" id="withCustomReturn">Return with custom return in Ajax</a>
</div>
<script>
    document.addEventListener("DOMContentLoaded", function (event) {
        $('#withCustomReturn').click(function () {
            let data = {
                'Test': 'test-1234',
                'Array': [1, 2, 3, 4],
                'sendError': false,
            };
            const RESULT = requestAjax("http://localhost:8888/components/ajax/ajax.php", data, null, null, null, 1, 0);
            if (RESULT.status) {
                console.log('My function here');
                console.log('You'r data :');
                console.log(RESULT.response)
                $('#result').html('<div class="alert primary m-b-2">n' +
                    'Check your console log' +
                    '</div>');
            }else{
                console.log('Error');
            }
        });
        $('[data-error]').click(function () {
            let data = {
                'Test': 'test-1234',
                'Array': [1, 2, 3, 4],
                'sendError': $(this).attr('data-error'),
        };
        requestAjax("http://localhost:8888/components/ajax/ajax.php", data, "#result", '<div class="alert success m-b-2">n' +
            '[message]' +
            '</div>', '<div class="alert danger m-b-2">n' +
            '[message]' +
            '</div>', 0, 1);
        });
    });
</script>
            

ajax.php


header('Content-type: application/json');
if(isset($_POST['data']['sendError']) && $_POST['data']['sendError'] === "false") {
    http_response_code(200);
    echo json_encode([
        'message' => 'Success',
        'data' => $_POST['data'],
    ]);
}else{
    http_response_code(400);
    echo json_encode([
        'message' => 'Error',
        'data' => $_POST['data'],
    ]);
}