Skip to content
Advertisement

Convert AJAX Jquery to Vanilla Javascript (POST) with .fetch async

I am trying to change this .ajax call from Jquery to pure Javascript.

And this way I receive the JSON in my PHP: echo '{"enviando":-1,"cat":"<span class=text-primary><strong>' . $exampleresult . '</strong></span>"}';

JQUERY CALL:

JavaScript

And this is my try with Vanilla Javascript:

But I get the error: SyntaxError: Unexpected end of JSON input at envSoli. And the error line: let resultado = await peticion.json();

What is the problem? How can I fix it? I’m just learning about JavaScript requests.

JavaScript

UPDATE

My textarea has 10 lines that with the Jquery code I would send each one to my PHP and then return them to the divs in HTML.

The current code (Vanilla), only makes 1 request and dont send the result to the HTML. (But my PHP validation is correct, the problem is in the JS code).

How i can fix it? I put a image for reference: img

Advertisement

Answer

After many attempts I have succeeded. This is the final result. (Thanks to the user @Barmar):

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement