项目是用cordova,ionic混合开发的,项目中使用cordova-plugin-http 插件来请求服务器端的https接口。开始始终访问不通过,服务端说请求参数语法不对,服务端需要的数据格式是JSON方式。目前测试的服务端是以IP 地址形式,服务端没有配证书。访问方式如下:
this.http.validateDomainName(false); //因为是服务点接口是ip 形式,没有配证书,所以设置不验证域名
return this.http.post(AppConfig.httpname+ url, body,// body是标准的json 格式body={"key1":"value1"}
{}).then(data =>JSON.parse(data.data)) //如果设置Header,该行的{}可以不改,在return上一行写this.http.setHeader("Content-Type","application/json");等设置headers
.catch(error => {
console.log(error.error);
});
以上测试不通过,因为服务端使用的是JSON格式,cordova-plugin-http 插件封装的就是native的AFNetworking第三方HTTP请求库,所以需要设置
AFHTTPSessionManager *manager;
manager.requestSerializer= [AFJSONRequestSerializer serializer];
查看到cordova-plugin-http 封装的接口,没有看到设置选项,在看CordovaHttpPlugin.m文件,发现
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager {
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[headersenumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
}];
}
该插件默认的是AFHTTPRequestSerializer,所以调试源码的时候始终报
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server code 500
内部服务错误,就是服务器收到请求了,但是请求数据格式不正确,
1. 要不就是你的JSON请求格式不正确,或不是json数据格式(有可能是字符串)等
2. 设置请求格式不正确,即requestSerializer等设置
看一下native怎么解决这个问题的,
即
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager {
//manager.requestSerializer = [AFHTTPRequestSerializer serializer];//注释掉即可
manager.requestSerializer= [AFJSONRequestSerializerserializer];
[headersenumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
[manager.requestSerializersetValue:objforHTTPHeaderField:key];
}];
}
我自己也fork一份github上的代码,可以参考
修改源码部分
1. 目录cordova-HTTP/www/cordovaHTTP.js中
requestSerializerType:function(typeIdent,success,failure) {
returnexec(success, failure,"CordovaHttpPlugin","requestSerializerType", [type]);
}
和
requestSerializerType:function(type) {
returnmakePromise(http.requestSerializerType, [type]);
}
2. 目录cordova-HTTP/src/ios/CordovaHttpPlugin.m
@implementationCordovaHttpPlugin{
AFSecurityPolicy *securityPolicy;
NSIntegerrequestSerializerType;
}
和
- (void)setRequestHeaders:(NSDictionary*)headersforManager:(AFHTTPSessionManager*)manager{
if(requestSerializerType ==1) {//AFJSONRequestSerializer
manager.requestSerializer= [AFJSONRequestSerializerserializer];
}elseif(requestSerializerType ==2){//AFPropertyListRequestSerializer
manager.requestSerializer= [AFPropertyListRequestSerializerserializer];
}else{//default AFHTTPRequestSerializer
manager.requestSerializer= [AFHTTPRequestSerializerserializer];
}
[headersenumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
[manager.requestSerializersetValue:objforHTTPHeaderField:key];
}];
}
- (void)requestSerializerType:(CDVInvokedUrlCommand*)command{
CDVPluginResult* pluginResult =nil;
requestSerializerType = [[command.argumentsobjectAtIndex:0]integerValue];
pluginResult = [CDVPluginResultresultWithStatus:CDVCommandStatus_OK];
[self.commandDelegatesendPluginResult:pluginResultcallbackId:command.callbackId];
}