react native 与服务器交互坑

来源:互联网 发布:知乎 法国街头 编辑:程序博客网 时间:2024/06/05 04:26

今天遇到两个坑

一个是以表单formData封装的JSON格式数据不是标准的json格式,导致服务器解析数据是错误返回400 bad request。例如


let formData = new FormData();    formData.append("api_account", 'iphone');    formData.append("timestamp", (new Date()).valueOf());    formData.append("username","user");    formData.append("password",'1111');     console.log("sent数据:" + JSON.stringify(formData));
sent数据:{"_parts":[["api_account","iphone"],["timestamp",1512961607281],["username","user"],["password","1111"]]}

后来改了一种方式可以了

let formdata= JSON.stringify({        "api_account": "iphone",        "timestamp": (new Date()).valueOf(),        "username": 'user',        "password": '1111',    });     console.log("==JSON==:" + formdata);


二是iOS模拟器10.3版本访问http协议有限制,

据说引入了新特性App Transport Security (ATS)。详情:App Transport Security (ATS)

新特性要求App内访问的网络必须使用HTTPS协议。

但是现在公司的项目使用的是HTTP协议,使用私有加密方式保证数据安全。现在也不能马上改成HTTPS协议传输。

1、在Xcode中修改Info.plist中添加NSAppTransportSecurity类型Dictionary
2、在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
3、iOS10以后版本的童鞋,注意了:NSAppTransportSecurity下不要有其他的key,否则NSAllowsArbitraryLoads会被忽略的.
In iOS 10 and later, and macOS 10.12 and later, the value of this key is ignored if any of the following keys are present in your app’s Info.plist file:NSAllowsArbitraryLoadsInMediaNSAllowsArbitraryLoadsInWebContentNSAllowsLocalNetworking 

https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW60




新手不懂道路深,摸索半天


阅读全文
0 0
原创粉丝点击