在post post中,如果我使用 x-www-form-urlencoded 传递数据,它会给我成功的响应,但同样的事情我尝试使用 afnetworking 然后它对我不起作用。
我知道这个问题已经回答了,但是有些人已经很老了,使用的方法已被弃用了,有些我试过但不适合我
我的代码如下
static func calledApi(withApiName strName:String,withParameter dictData:NSDictionary,withSuccessBlock objSuccess:successBlock,withFailureBlock objFailure:failureBlock){
strBaseUrl = strBaseUrl.stringByAppendingString(strName)
let manager = AFHTTPSessionManager(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
manager.requestSerializer.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
// manager.requestSerializer.setQueryStringSerializationWithBlock { (request, id,error) -> String in
// let jsonData = try! NSJSONSerialization.dataWithJSONObject(dictData, options: .PrettyPrinted)
// let myString = String(data: jsonData, encoding: NSUTF8StringEncoding)
// return myString!
// }
// manager.requestSerializer = AFHTTPRequestSerializer()
manager.responseSerializer = AFHTTPResponseSerializer()
manager.POST(strBaseUrl, parameters: dictData, progress: { (progress) in
}, success: { (session, responceObj) in
let datastring : String = String(data: (responceObj as! NSData), encoding: NSUTF8StringEncoding)!
print(datastring)
var dictData = NSDictionary()
do {
dictData = try NSJSONSerialization.JSONObjectWithData((responceObj as! NSData), options: []) as! [String:AnyObject]
} catch let error as NSError {
print(error)
return
}
if Int(dictData.valueForKey("code") as! String) == 1
{
objSuccess(dictData)
}
else
{
objFailure(nil, dictData.valueForKey("message") as! String)
}
}) { (session, error) in
print(error)
objFailure(error,nil)
}
}