• Hello,
    Hope you are doing good.

    I need your help if you may, please.
    I have a js code that actually generate a pdf client side and should supposely upload it on the wordpress server. It’s not the first time i do that. It usually work, and the exact very same code work on another wordpress web-site i have. But for some reason, doesn’t work this time.
    Both of the server are astra theme also, i don’t know if it help but still it won’t work this time and i don’t know why. The code is pretty simple :

            `const currentDate = new Date();
    const dateFormat = ${("0" + (currentDate.getMonth()+1)).slice(-2)}/${("0" + currentDate.getDate()).slice(-2)} - ${("0" + currentDate.getHours()).slice(-2)}h${("0" + currentDate.getMinutes()).slice(-2)};
            const xhr = new XMLHttpRequest();
            xhr.open("POST", "https://eima.school/wp-admin/admin-ajax.php", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");`
    
    
           xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        console.log("PDF uploaded successfully");
                        aenvoyer[1].save(${aenvoyer[2]} - ${aenvoyer[3]} - Inscription - Stages - EIMA - 2023 - ${dateFormat}.pdf);
                        event.target.submit();
                    } else {
                        alert("PDF upload failed with status " + xhr.status + " and response " + xhr.responseText);
                    }
                }
            };
            aenvoyer[1].save(${aenvoyer[2]} - ${aenvoyer[3]} - Inscription - Stages - EIMA - 2023 - ${dateFormat}.pdf);
            xhr.send(action=upload_pdf&pdfBase64=${encodeURIComponent(aenvoyer[0])}&nom=${encodeURIComponent(aenvoyer[2])}&prenom=${encodeURIComponent(aenvoyer[3])});

    I actually put the xhr.send two time voluntarily just to see if the pdf would download and it does perfectly. The only problem is it doesn’t upload anything on the server side with the error : PDF upload failed with status 400 and response 0.
    Can someone help ? I have no idea what is actually happening.
    I would suppose that something is going wrong in the admin-ajax, maybe the post method ? I don’t know how to check it or what to do. Usually when i code things like that it just work so…

    Thanks all for your help

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Usually a 400 response from admin-ajax.php is because there’s no action callback related to your action parameter. You should have PHP code somewhere adding your callback to action “wp_ajax_upload_pdf” if logged in and “wp_ajax_nopriv_upload_pdf” if not.

    If there’s an error in your callback you might get the same response even if it was correctly added. Debugging an Ajax callback from xhr is difficult. You can setup a PHP test bed on a custom page template. Have your PHP assign appropriate values to $_REQUEST, then directly call the Ajax callback function, or execute it with do_action().

    You’ll also need to alter your callback code to have it output debug data that will be visible on the test bed page. It can be helpful to define WP_DEBUG as true so you can see any error messages right on the test bed page.

Viewing 1 replies (of 1 total)
  • The topic ‘Xhr.send : PDF upload failed with status 400 and response 0’ is closed to new replies.