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

Add AWS Cognito to angular application

1  Asked on November 16, 2021 by captainmorgan

     

How to analize which object owns the max memory

0  Asked on November 16, 2021 by loverszhaokai

         

enable_if, SFINAE and template parameters?

2  Asked on November 16, 2021

 

Dynamic ‘wait’ arg in Scrapy-splash

0  Asked on November 16, 2021 by winters

       

Grab a decimal between 2 strings regex

1  Asked on November 15, 2021 by lenny-gonzalez

 

Django , models object not displayed in views

2  Asked on November 15, 2021 by mxzero-mxzero

     

Re-render a sibling component using hooks

3  Asked on November 15, 2021 by programmer1

     

filter method returns empty array

3  Asked on November 15, 2021

     

Fit container inside image

1  Asked on November 15, 2021

 

Ask a Question

Get help from others!

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