最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - beforeSend: function() in fetch API - Stack Overflow

programmeradmin4浏览0评论

I am currently migrating the use of ajax to the fetch, however I am needing a parameter/function the Ajax native beforeSend: function(). I would like to perform the same action without requiring an implementation in fetch (a creation of a new class for example)

jQuery $.ajax structure:

$.ajax({
    url: '...',
    type: 'post',
    data: {...},
    beforeSend: function() {
        // perform the action..
    },
    success: function(response, status, xhr) {
        // receive response data
    }
});

JavaScript fetch structure:

fetch('...').then((response) => {
    return response.text();
}).then((data) => {
    console.log(data);
});

How do I determine such a function without needing an implementation or creating a new class on fetch. Is there any means or only implementation? because of my searches I only found implementations like CustomFetch (Is there a beforesend Javascript promise) and others.

I am currently migrating the use of ajax to the fetch, however I am needing a parameter/function the Ajax native beforeSend: function(). I would like to perform the same action without requiring an implementation in fetch (a creation of a new class for example)

jQuery $.ajax structure:

$.ajax({
    url: '...',
    type: 'post',
    data: {...},
    beforeSend: function() {
        // perform the action..
    },
    success: function(response, status, xhr) {
        // receive response data
    }
});

JavaScript fetch structure:

fetch('...').then((response) => {
    return response.text();
}).then((data) => {
    console.log(data);
});

How do I determine such a function without needing an implementation or creating a new class on fetch. Is there any means or only implementation? because of my searches I only found implementations like CustomFetch (Is there a beforesend Javascript promise) and others.

Share Improve this question edited Jul 19, 2021 at 1:56 1nslg asked Feb 6, 2021 at 17:55 1nslg1nslg 1901 silver badge17 bronze badges 6
  • What does your beforeSend method logic looks like? – MinusFour Commented Feb 6, 2021 at 18:01
  • @MinusFour As well? for the current application? – 1nslg Commented Feb 6, 2021 at 18:04
  • Yes, how do you actually use it. jQuery basically uses this method to access a version of a XHR object. The interface is very different with fetch. – MinusFour Commented Feb 6, 2021 at 18:10
  • So do you think I should handle the actions before the end with XHR object? – 1nslg Commented Feb 6, 2021 at 18:15
  • No, there's no XHR object with fetch. It's a different interface. Whatever you did with the XHR object inside beforeSend will most likely have a different way to do so with fetch. – MinusFour Commented Feb 6, 2021 at 18:35
 |  Show 1 more ment

1 Answer 1

Reset to default 5

Solved problem!

No need to implement and/or create a new fetch class. Using only parameters as "function inside function". I hope you can help others!

function ipGetAddress(format) {
  requestFetch = function() {
    // perform the action..
    console.log('** beforeSend request fetch **');
    return fetch.apply(this, arguments);
  }

  requestFetch(`https://api.ipify?format=${format}`).then((response) => {
    return response.json();
  }).then((data) => {
    console.log(data.ip);
  });
}

ipGetAddress('json') // return local ip address..

发布评论

评论列表(0)

  1. 暂无评论