問題
新搭建的一個項目,在使用fetch進行網絡請求的時候遇到一個報錯:
TypeError: Network request failed.
嘗試直接在瀏覽器打開url訪問可以正常拿到返回值。(url是http協議)
原因
這個API請求是http的協議請求,在iOS9以上系統,已經把http協議的請求限制了。
iOS9引入了新特性App Transport Security (ATS)。詳情:App Transport Security (ATS)
新特性要求App內訪問的網絡必須使用HTTPS協議。
解決方案
- 在
xcode中打開react native中的ios項目根目錄; - 打開項目下的info.plist文件;
- 在
App Transport Security Settings下新增Allow Arbitrary Loads,值為YES;
- 重啓react native項目,刷新,請求後返回值正常;