React fetch polyfill 在移动设备上不起作用

React fetch polyfill on mobile devices not working

正在尝试让 fetch 在 ios/android(chrome 和 safari)上工作。 我目前在我的 webpack 中有这个(使用 webpack 4):

new webpack.ProvidePlugin({
   Promise: 'imports-loader?this=>global!exports-loader?global.Promise!es6-promise',
   fetch: 'imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch'
}),

此代码适用于 IE 桌面,但在我的 android 上它给出以下错误:

Uncaught (in promise) TypeError: Failed to fetch

获取代码:

fetch('http://localhost:8092/api/shops/search?searchString=spicy')
    .then(function(response) {
        if (response.status >= 400) {
            throw new Error("Bad response from server");
        }
        return response.json();
    })
    .then(function(stories) {
        console.log('hej', stories);
    });

有什么想法吗?

这是因为您的 http://localhost:8092 在您的桌面上正确解析(假设为 127.0.0.1),但对于您的移动设备 - localhost 是这个特定的设备,而不是您的桌面。所以我想你应该尝试使用可以通过你的所有网络访问的 ip 地址(而不是仅仅 localhost),假设你的移动设备连接到同一个。