AnswerBun.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!

Related Questions

Validation form with optional fields react-hook-forms

1  Asked on September 10, 2020 by th3g3ntl3man

     

Another problem with unload function, node set to null is repopulating somehow

1  Asked on September 9, 2020 by pleasant-nightmares

   

get signature from TokenValidatedContext

2  Asked on September 8, 2020 by olaf-svenson

       

Boolean expression ( 0 or None and False)

1  Asked on September 7, 2020 by una

   

Asp.net Identity Role in Razor Pages

1  Asked on September 7, 2020

     

mod_fcgid: read data timeout in 40 seconds

2  Asked on September 6, 2020 by complexi

 

Evaluate Xquery in pyspark on RDD elements

1  Asked on September 6, 2020 by fahad-rana

       

Get only values in brackets after row splitting using awk

3  Asked on September 5, 2020 by valentyn

   

python program for wifi connected devices

1  Asked on September 5, 2020 by utkarsh-trivedi

 

How to tell if in a synchronised block in Java?

2  Asked on September 2, 2020 by 8483315

   

Add GoLang syntax highlighting for VIM

11  Asked on September 1, 2020 by rocketas

       

Ask a Question

Get help from others!

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