iOS React Native fetch()POST:网络请求失败 [英] IOS React Native fetch() POST : Network Request Failed

查看:169
本文介绍了iOS React Native fetch()POST:网络请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)以下代码在IOS上给我一个错误,但在Android上可以使用.

1) The following code give me an error on IOS but work on Android.

2)在IOS上,我可以使用获取"进行获取,但发布"失败

2) On IOS i Can make fetch with "get" but "post" fails

所以这在ios上不起作用:

so this don't work on ios :

    var data = new FormData();
    data.append("fileUpload", { uri: imageUri, name: filename, type: "image/jpeg" });
    data.append("filename", filename);
    data.append("name", "uploadedFile");
    const config = {
        method: "POST",
        headers: {
            Accept: "application/json",
            "Content-Type": "multipart/form-data;"
        },
        body: data
    };

    return fetch(url, config).then((response) => response.json())
        .then(res => { return res ;})
        .catch(error => {
            console.error(error);
            return { name: "network error", description: "" };
        });

我尝试根据其他帖子(例如 React)在IOS上修复info.plist本机fetch()网络请求失败),但错误仍然存​​在

i try to fix the info.plist on IOS according to other post (like React Native fetch() Network Request Failed) but the error still there

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        <key>CFBundleDisplayName</key>
        <string>myapp</string>
        <key>CFBundleExecutable</key>
        <string>$(EXECUTABLE_NAME)</string>
        <key>CFBundleIdentifier</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>$(PRODUCT_NAME)</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
        <string>1</string>
        <key>LSRequiresIPhoneOS</key>
        <true/> 
        <key>NSAppTransportSecurity</key>                   // i add this
        <dict>                                              // i add this
            <key>NSAllowsArbitraryLoads</key>               // i add this
            <true/>                                         // i add this
            <key>NSAllowsArbitraryLoadsInWebContent</key>   // i add this  
            <true/>                                         // i add this
        </dict>                                             // i add this 
        <key>NSCameraUsageDescription</key>
        <string>Your message to user when the camera is accessed for the first time</string>
        <key>NSLocationWhenInUseUsageDescription</key>
        <string></string>
        <key>NSMicrophoneUsageDescription</key>
        <string>Your message to user when the microsphone is accessed for the first time</string>
        <key>NSPhotoLibraryUsageDescription</key>
        <string>Your message to user when the photo library is accessed for the first time</string>
        <key>UIAppFonts</key>
        <array>
            <string>Zocial.ttf</string>
            <string>Feather.ttf</string>
        </array>
        <key>UILaunchStoryboardName</key>
        <string>LaunchScreen</string>
        <key>UIRequiredDeviceCapabilities</key>
        <array>
            <string>armv7</string>
        </array>
        <key>UISupportedInterfaceOrientations</key>
        <array/>
        <key>UIViewControllerBasedStatusBarAppearance</key>
        <false/>
        <key>LSApplicationCategoryType</key>
        <string></string>
    </dict>
    </plist>

我尝试重新启动xcode,清理项目并构建所有项目.

i try to restart xcode, clean project and build all.

有什么建议吗?谢谢

推荐答案

iOS不允许不安全的HTTP请求.为此,您必须将域添加为info.plist内的例外.

iOS does not allow insecure HTTP requests. In order to do so, you have to add your domain as an exception inside your info.plist.

添加以下内容:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

这篇关于iOS React Native fetch()POST:网络请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆