0

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

6
  • What's the PHP code that handles the AJAX request? Oct 11, 2023 at 13:44
  • also have you tried more modern methods of doing this such as Fetch on the JS side, and using the REST API on the PHP side instead of the legacy admin ajax? The HTTP 400 and body response of 0 indicates you've made a basic mistake with using the old admin ajax API and WP doesn't have a handler for this AJAX action for the current user, but there's no PHP code to confirm this or refute it
    – Tom J Nowell
    Oct 11, 2023 at 13:55
  • @JacobPeattie Hello, thanks for helping me. I think the exact problem is what you just pointed. I have no idea/i don't remember what or where is the php code. I've been using this js code on multiple website for multiple years and i i've always used it like that. It always worked so, in fact, i would say i don't even know what is the php code that handle it. I suppose it should be something in function.php since we are on wordpress but... i don't know exactly lol. Again this exact same code works, actually, on other website that use same theme on top of that :/ Oct 11, 2023 at 15:48
  • @TomJNowell Hi tom, thanks for helping me. I didn't try any other method cause i'm not sure i would be able to. I mean, if it was on a normal website i did develop myself i would be able to do it. But on wordpress i rarely mess with the backend. I'll do some research and see if i could try another approach to do it. But, what i don't understand is : this exact same code works, at this moment, on other website that use same theme on top of that lol. And as for the php code i'm unsure, i supposed it should be something in the functions.php but i can't find anything to help me there Oct 11, 2023 at 16:06
  • Did you write this code? Without seeing the PHP side of this AJAX it's impossible to help you. If you have written this javascript but have not written any PHP to handle it at the other end then the answer is simple, you need to write PHP to handle it at the other end and the 400/0 is WordPress telling you it doesn't know what to do with the request.
    – Tom J Nowell
    Oct 11, 2023 at 16:17

1 Answer 1

0

failed with status 400 and response 0

When you make a request to admin-ajax.php you need to pass an action parameter, which here is upload_pdf. If nothing on the PHP side handles this then admin-ajax.php will return a 0 and a HTTP status of 400.

Essentially, WordPress is saying it has no idea what you're talking about.

Looking at the comments it seems you've copied this javascript across sites expecting it to work on its own. These other sites have an admin AJAX handler in their PHP that is missing from this site. This could be because it wasn't copied over, a new theme, a missing plugin, etc.

You will need to search an older working site and discover the location of the admin ajax handler. This is not something that people online can help you with as it requires searching your servers files. Once you have done this you will have your answer for how to fix it, e.g. you found it in a different theme, or in a plugin you forgot to install/activate.

3
  • What you are saying seems fully logic for me. But i can't understand why exactly this website in particular doesn't have a upload_pdf ? I did write the js, but i never had previously to write anything else. It's like upload_pdf always existed in wordpress but for the first time it doesn't exist. So i don't really know how to find it since i never had to find it previously. I'll be searching it but i have to admit i'm confused lol. Oct 11, 2023 at 16:30
  • 1
    An upload_pdf AJAX action has never existed in WordPress. There must be a common plugin or theme you've been using that provided it. Oct 12, 2023 at 8:56
  • @JacobPeattie Turns out (unsusprisingly i guess ?) that you are just right lmao. I didn't use this code in a year or so I took the entire night looking at every functions.php on the webite where it was working. After not finding anything, i found that, in fact, i had myself program a php plugin that was handling the pdf upload, lol. And in this function php, i actually created a handler for upload_pdf. So you are just right and my memory was wrong. Thanks a lot Oct 12, 2023 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.