Fetch request with ASP . Net Core 3.0

Asked

Viewed 127 times

2

I am trying to replace the Ajax methods by Fetch in the application, but I cannot receive an object with Fetch, I made a small test object to try to receive, in Ajax, with you passing parameter through Data

    public class ObjetoTeste
    {
        public string Nome { get; set; }
        public string NomeFantasia { get; set; }
    }

On my controller, I receive as follows

    public JsonResult Teste(long s, ObjetoTeste obj)
    {
        return Json(obj);
    }

With Ajax, I receive the object normally, already in Fetch, I can’t send anything, it always receives null in Controller, in Javascript, I set up 2 methods that receive the same object, identical, and both behave differently.

    var data = {
        s: 500,
        obj: {
            Nome: 'Nome',
            NomeFantasia: 'NomeFantasia'
        }
    }

    _private.Ajax = function () {
        console.log('Ajax')
        $.ajax({
            url: "/Home/Teste",
            type: "POST",
            data: data,
            success: function (response) {
                console.log(response)
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus, errorThrown);
            }
        });
    }

    _private.Fetch = function () {
        console.log('Fetch')
        fetch('/Home/Teste', {
            method: 'POST',
            body: data,
        }).then((resp) => resp.json())
        .then(function (data) {
            console.log(data)
        })
    }

It’s just an example snippet, but if it works out, I will implement the Fetch method as default in my requests.

  • what you have in the object data? that extra comma is unnecessary here: body: data,

  • data is the object I created to send, like this code I posted, and even without the comma it arrives null in the controller.

  • humm had not seen the date object... tried to specify in the header that it is a json?, adding for example this before the date: headers: {
 'Accept': 'application/json',
 'Content-Type': 'application/json'
 },

  • I was able to partially solve, not the way I wanted, I had to swap the parameter in the Controller with the [Frombody] tag, but the s still have to pass as a parameter in the url, fetch('/Home/Test/? s=500', which makes it impossible to pass multiple objects as parameters, wouldn’t have a way to use Fetch as if it were Ajax? Or Fetch does not replace Ajax features yet?

  • in this case vc has two solutions to send a single object as [FromBody: bring it all together in one class (ObjetoTeste also have the long s, or join the two in a third class), or receive an object of the type JObject, and then separate the s and the ObjetoTeste. If you want I can answer with an example

  • This operation would not meet, I in case would change the Ajax requests of an ERP, I thought that the operation would be similar, would be more for performance test than real need of the exchange, but feel free to answer with example, I will mark your reply as accepted, thank you very much for your help.

  • @Christianribeiro Make both requests, take the payloads from the browser console and compare them. If you can’t find the problem, show the two payloads on your question so we can see the difference between them.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.