TransWikia.com

AJAX to fetch, get same funcionality

Stack Overflow Asked by Reinaldo EP on January 3, 2022

i want to know how can i get the same funcionality from ajax jquery to fetch javascript

this is my ajax.

$.ajax({
    url: url,
    type: "POST",
    data: data, //FormData
    contentType: false,
    processData: false,
    success: function(result) {
        console.log(result);
    },
    error(e) {
        console.log(e.responseJSON.message); //<---this is what i want in my fetch :)"this is my error text"
    }
});

and there my fetch

options = {
            method: "POST",
            body: data // FormData
        }
const response = await fetch(url, options);
if(!response.ok){
     return response.statusText;//always "internal server error" i need my custom msg for user ;(
}


my backend in laravel

abort(500, "this is my error text");

One Answer

You have to use response.text() to get the actual response body

options = {
        method: "POST",
        body: data // FormData
}
const response = await fetch(url, options);
if(!response.ok){
    return response.text();
}

Answered by dave on January 3, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP