項(xiàng)目是用cordova,ionic混合開發(fā)的匙赞,項(xiàng)目中使用cordova-plugin-http 插件來(lái)請(qǐng)求服務(wù)器端的https接口文判。開始始終訪問(wèn)不通過(guò)上祈,服務(wù)端說(shuō)請(qǐng)求參數(shù)語(yǔ)法不對(duì)藐守,服務(wù)端需要的數(shù)據(jù)格式是JSON方式夺衍。目前測(cè)試的服務(wù)端是以IP 地址形式狈谊,服務(wù)端沒(méi)有配證書。訪問(wèn)方式如下:
this.http.validateDomainName(false); //因?yàn)槭欠?wù)點(diǎn)接口是ip 形式,沒(méi)有配證書河劝,所以設(shè)置不驗(yàn)證域名
return this.http.post(AppConfig.httpname+ url, body,// body是標(biāo)準(zhǔn)的json 格式body={"key1":"value1"}
{}).then(data =>JSON.parse(data.data)) //如果設(shè)置Header,該行的{}可以不改壁榕,在return上一行寫this.http.setHeader("Content-Type","application/json");等設(shè)置headers
.catch(error => {
console.log(error.error);
});
以上測(cè)試不通過(guò),因?yàn)榉?wù)端使用的是JSON格式赎瞎,cordova-plugin-http 插件封裝的就是native的AFNetworking第三方HTTP請(qǐng)求庫(kù),所以需要設(shè)置
AFHTTPSessionManager *manager;
manager.requestSerializer= [AFJSONRequestSerializer serializer];
查看到cordova-plugin-http 封裝的接口牌里,沒(méi)有看到設(shè)置選項(xiàng),在看CordovaHttpPlugin.m文件务甥,發(fā)現(xiàn)
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager {
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[headersenumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
}];
}
該插件默認(rèn)的是AFHTTPRequestSerializer牡辽,所以調(diào)試源碼的時(shí)候始終報(bào)
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server?code 500 ?
內(nèi)部服務(wù)錯(cuò)誤,就是服務(wù)器收到請(qǐng)求了敞临,但是請(qǐng)求數(shù)據(jù)格式不正確态辛,
1. 要不就是你的JSON請(qǐng)求格式不正確,或不是json數(shù)據(jù)格式(有可能是字符串)等
2. 設(shè)置請(qǐng)求格式不正確挺尿,即requestSerializer等設(shè)置
看一下native怎么解決這個(gè)問(wèn)題的奏黑,
即
- (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];
}