React Fetch not working in IE11

删除回忆录丶 提交于 2019-12-03 10:29:46

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

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

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.

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

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

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/.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!