我在我的应用程序中使用Alamofire进行网络连接。在Alamofire中,我正在编写POST方法将值传递给url,但它没有传递任何值。显示为null。以下是我的代码:
<input type="button" value="Submit" onclick="changeConfirmed(json_encode($result))">
如果有人弄错了我做错了什么。谢谢!!
答案 0 :(得分:5)
您使用的是哪种版本的alamofire? 请尝试使用URLEncoding.default而不是JSONEncoding.default。
答案 1 :(得分:0)
试试这样:
let url = NSURL(string:URLName)
让request = NSMutableURLRequest(URL:url!)
request.HTTPMethod =“POST”
request.setValue(“application / json”,forHTTPHeaderField:“Content-Type”)
让data =试试! NSJSONSerialization.dataWithJSONObject(参数!,选项:NSJSONWritingOptions.PrettyPrinted)
让json = NSString(数据:数据,编码:NSUTF8StringEncoding) 如果让json = json { 打印(JSON) }
request.HTTPBody = json!.dataUsingEncoding(NSUTF8StringEncoding);
Alamofire.request(request)
.responseString { response in
// do whatever you want here
print(response.result)
switch (response.result) {
case .Success(let JSON):
print(JSON)
case .Failure: break
}
}
答案 2 :(得分:0)
我使用swift2.3并使用Alamofire ..它对我有用:
class APIUtils: NSObject{static func fetchData(requestModel:RequestModel,completionBlock:(response:ResponseModel)->Void){
let url = "\(requestModel.baseUrl)\(requestModel.apiUrl)"
Alamofire.request(APIUtils().getHTTPMethod(requestModel.httpMethodType), url, parameters: requestModel.parametersDict,headers:requestModel.headersDict,encoding:(APIUtils().getEncodingFormat(requestModel.encodeType))).responseJSON { (responseObj) -> Void in
let responseModel = ResponseModel()
responseModel.responseCode = responseObj.response?.statusCode
switch responseObj.result {
case .Success(let data):
if (data .isKindOfClass(NSDictionary)){
responseModel.responseDictionary = data as! NSDictionary
}else if (data .isKindOfClass(NSArray)){
responseModel.customModel = data as! NSArray
}
responseModel.isSuccess = true
break
case .Failure(let error):
responseModel.isSuccess = false
print("Request failed with error: \(error)")
break
}
completionBlock(response: responseModel)
}
}
func getHTTPMethod(methodType:HTTPMethodType)->Alamofire.Method{
switch(methodType){
case .GET: return Alamofire.Method.GET
case .POST: return Alamofire.Method.POST
case .PUT: return Alamofire.Method.PUT
case .PATCH: return Alamofire.Method.PATCH
case .DELETE: return Alamofire.Method.DELETE
}
}
func getEncodingFormat(methodType:encodingType)->Alamofire.ParameterEncoding{
switch(methodType){
case .URL: return Alamofire.ParameterEncoding.URL
case .JSON: return Alamofire.ParameterEncoding.JSON
case .URLEncodedInURL: return Alamofire.ParameterEncoding.URLEncodedInURL
}
}
}
//试试这个:
let urlencode = Alamofire.ParameterEncoding
print(MyStrings.BaseURl+urlString)
Alamofire.request(action, MyStrings.BaseURl+urlString, headers: headers, parameters:urlParams, encoding: isBody == true ? urlencode.JSON : urlencode.URLEncodedInURL)
.validate(statusCode: 200..<300)
.responseJSON { responseO in
if (responseO.result.error == nil) {
let responseModel = ResponseModel()
responseModel.responseCode = responseO.response?.statusCode
switch responseO.result {
case .Success(let data):
if (data .isKindOfClass(NSDictionary)){
responseModel.responseDictionary = data as! NSDictionary
}else if (data .isKindOfClass(NSArray)){
responseModel.customModel = data as! NSArray
}
responseModel.isSuccess = true
break
case .Failure(let error):
responseModel.isSuccess = false
print("Request failed with error: \(error)")
break
}
completionBlock(response: responseModel)
}
else {
debugPrint("HTTP Request failed: \(responseO.result.error?.userInfo)")
let responseModel = ResponseModel()
responseModel.responseCode = responseO.response?.statusCode
responseModel.isSuccess = false
completionBlock(response: responseModel)
}
}