react native 与服务器交互坑

今天遇到两个坑

一个是以表单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)

扫描二维码关注公众号,回复: 4640311 查看本文章

新特性要求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: NSAllowsArbitraryLoadsInMedia NSAllowsArbitraryLoadsInWebContent NSAllowsLocalNetworking 

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




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


猜你喜欢

转载自blog.csdn.net/s349856186/article/details/78770560
今日推荐