• ostaladafab

    (@ostaladafab)


    I need your help. I’m not sure if it’s about WordPress actually. I’m currently developing a form scratch theme on which I need to send an AJAX request. Everything works fine in local. But not in pre-production where I get a 404 error. The idea is to :
    – Create a canvas
    – To get the data URL of the canvas in JS
    – To send it in Ajax
    – To save this data in a custom field

    Here is the HTML

    <canvas id="canvas" width="700" height="700"></canvas>
    <img src="/path-to-image/background.png" />

    Here is the JS

    let canvas = document.getElementById("canvas");
    let background = document.getElementById("background");
    let ctx = canvas.getContext("2d");
    
    image.addEventListener("load", () => {
      ctx.drawImage(background, 0, 0, 700, 700);
      ctx.fillText("John", 170, 381);
      ctx.fillText("Doe", 205, 401);
    
      let endpoint = "/path-to-file/admin-ajax.php";
      let form_data = new FormData();
      form_data.append("action", "set_canvas");
      form_data.append("canvas", canvas.toDataURL());
      axios
        .post(endpoint, form_data)
        .then((response) => {
          console.log(response.data);
        })
        .catch((error) => {
          console.log(error);
        });
    });

    Here is the PHP

    function set_canvas() {
    
        $response = array(
            'test' => 'ok',
        );
    
        echo wp_send_json($response);
        wp_die();
    }
    
    add_action( 'wp_ajax_set_canvas', 'set_canvas' );
    add_action( 'wp_ajax_nopriv_set_canvas', 'set_canvas' );

    Here is what the console displays in local
    {test: 'ok'}

    Here is what the console displays in pre-production

    Error: Request failed with status code 404
    at e.exports (createError.js:16)
    at e.exports (settle.js:17)
    at XMLHttpRequest.E (xhr.js:66)

    It works in local and in preproduction if I modify slightly the JS. Namely, if I pass any string to the “canvas” argument. Like this:

    let canvas = document.getElementById("canvas");
    let background = document.getElementById("background");
    let ctx = canvas.getContext("2d");
    
    image.addEventListener("load", () => {
        ctx.drawImage(background, 0, 0, 700, 700);
        ctx.fillText("John", 170, 381);
        ctx.fillText("Doe", 205, 401);
    
        let endpoint = "/chemin-vers-le-fichier/admin-ajax.php";
        let form_data = new FormData();
        form_data.append("action", "set_canvas");
        form_data.append("canvas", "string"); // <--- Here
        axios
            .post(endpoint, form_data)
            .then((response) => {
                console.log(response.data);
            })
            .catch((error) => {
                console.log(error);
            });
       });

    Do you have a solution?
    I look forward to reading you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The endpoint path is incorrect for a production server. It should be a full fledged URL to admin-ajax.php. For example https://example.com/wp-admin/admin-ajax.php.

    In general I recommend avoiding any relative paths in WP. Always use full, absolute paths. The WP permalink scheme can cause relative paths to fail.

    Thread Starter ostaladafab

    (@ostaladafab)

    Hello bcworkz,

    Thanks for your answer. But it is not a relative path problem. I rewrote my script for this topic. In the original script, the endpoint is full fledged. I get it from admin_url('admin-ajax.php' ), then pass it to my script with the wp_localize_script() function.

    I re-saved my permalinks configuration in the pre-production settings. Still the same problem.

    another solution ?

    Moderator bcworkz

    (@bcworkz)

    If you’re sure the URL is correct, there’s still a server configuration issue if it’s returning 404 status. What happens if you request the Ajax URL from a browser, appending ?action=set_canvas to the URL to correctly route to your callback. I would still expect 404, but if you get {test: ok} then there’s something odd about how the script makes its request (check the headers).

    I’ve no good idea why it’d 404 on a valid URL. Maybe modSecurity at play? It normally returns 403, but I’ve at times encountered 404 due to its behavior.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘404 ajax error in preproduction but not in local’ is closed to new replies.