今天遇到两个坑
一个是以表单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
协议传输。
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
新手不懂道路深,摸索半天