First Fetch API call cancelled by the second in IE11

前端 未结 5 1067
鱼传尺愫
鱼传尺愫 2021-01-01 16:11

I have a ReactJS application that works as expected in Chrome, but fails in IE-11.

The problem is this - we have two drop down lists which are populated from rest ser

相关标签:
5条回答
  • 2021-01-01 16:23

    Unrelated to ReactJS, but I hope useful to others who land here as I did.

    I found the Github fetch polyfill and TaylorHakes' Promise polyfill nice and easy to get started with in the context of my ASP.NET MVC web application. Both were recommended by https://ourcodeworld.com

    0 讨论(0)
  • 2021-01-01 16:25

    IE dont offer suport for the fetch use. You need use some polyfill for solve this problem. You can use http://github.github.io/fetch/.

    0 讨论(0)
  • 2021-01-01 16:33

    As pointed out, fetch is not supported by IE11, I also had this problem in my React app. You can, alternatively, use Axios.

    If you are migrating (or can migrate), check My answer to this post

    Basically, check this code:

    Fetch JSON post request

    let url = 'https://someurl.com';
    let options = {
                method: 'POST',
                mode: 'cors',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json;charset=UTF-8'
                },
                body: JSON.stringify({
                    property_one: value_one,
                    property_two: value_two
                })
            };
    let response = await fetch(url, options);
    let responseOK = response && response.ok;
    if (responseOK) {
        let data = await response.json();
        // do something with data
    }
    

    Axios JSON post request

    let url = 'https://someurl.com';
    let options = {
                method: 'POST',
                url: url,
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json;charset=UTF-8'
                },
                data: {
                    property_one: value_one,
                    property_two: value_two
                }
            };
    let response = await axios(options);
    let responseOK = response && response.status === 200 && response.statusText === 'OK';
    if (responseOK) {
        let data = await response.data;
        // do something with data
    }
    

    I always have main request function that the app uses, so it doesn't matter if you use fetch or axios. For example in 'your-common.js':

    async function request(url, method, objectData){
        // use axios or fetch or isomorphic-fetch
    }
    

    So if your complete app is using your request function, you simply change the library in that function and no harm is done as long as you return the same object or value. You can also wrap the fetch (or axios or whatever http library you use) in a try/catch block to handle promises if using async/await.

    0 讨论(0)
  • 2021-01-01 16:35

    It seems that you are using the Fetch API which is not supported yet by IE. That's why your app works on Chrome.

    I would suggest you moving to a 3rd party lib such as Axios, superagent, etc...

    0 讨论(0)
  • 2021-01-01 16:39

    Just include isomorphic-fetch as polyfill to make it work on unsupported browsers.

    https://github.com/matthew-andrews/isomorphic-fetch

    0 讨论(0)
提交回复
热议问题