我想用post方法設置「application/x-www-form-urlencoded」 因此,我設置了request.requestSerializer.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
但是當我將請求設置到服務器時,Content-Type不會發生什麼變化?如何在AFNetworking中設置Content-Type?
這是錯誤
{com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7c166450> { URL: http://test.com } { status code: 404, headers {
Connection = "keep-alive";
"Content-Length" = 434;
"Content-Type" = "text/html";
Date = "Mon, 13 Jul 2015 11:18:18 GMT";
Server = "nginx/1.0.15";
} }, NSErrorFailingURLKey=http://text.com, NSLocalizedDescription=Request failed: not found (404),
這是我的代碼:
var request = AFHTTPRequestOperationManager();
request.requestSerializer = AFHTTPRequestSerializer()
request.requestSerializer.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.POST(url, parameters: parameters, success: { (oper,obj) -> Void in
// do something
}) { (oper, error) -> Void in
// do something with error
}
你張貼錯誤的'response'頭不是'request'頭。你可以用'println(「Headers:\(request.requestSerializer.HTTPRequestHeaders)」)來檢查頭文件是否被設置了。'進一步的錯誤是'404',這意味着你請求的頁面不存在。所以看看,而不是你的標題。 – sbarow
[User-Agent:yousi_online_answer/1.0(iPad Simulator; iOS 8.3; Scale/1.00),Accept-Language:en; q = 1,fr; q = 0.9,de; q = 0.8,zh-Hans; q = 0.7 ,zh-Hant; q = 0.6,ja; q = 0.5] @sbarow – jansma
@sbarow我已經在郵遞員中試過了,很好 – jansma