I am using AFNetworking to GET a plain/text:
let manager = AFHTTPRequestOperationManager()
manager.GET(url, parameters: nil, success: { (op: AFHTTPRequestOperation!, res: AnyObject!) -> Void in
},failure: { (op: AFHTTPRequestOperation!, er:NSError!) -> Void in
println(op,er)
})
The accept content types:
manager.responseSerializer.acceptableContentTypes
[text/javascript, application/json, text/json]
So I got error in failure block:
NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}
Then i added text/plain in this way:
var set = manager.responseSerializer.acceptableContentTypes
set.insert("text/plain")
manager.responseSerializer.acceptableContentTypes = set
right now the types are:
manager.responseSerializer.acceptableContentTypes
[application/json, text/javascript, text/plain, text/json]
But i got the new error:
{ URL: http://192.168.1.9:8081/sec.jsp } { status code: 200, headers {
"Content-Length" = 44;
"Content-Type" = "text/plain; charset=utf-8";
Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
Server = "Jetty(6.1.10)";
"Set-Cookie" = "JSESSIONID=tapmhct7hanv;Path=/";
} }>, Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fda31fb4a90 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})
I think the question is because JSON can't read the response. But i have set it to text/plain, does it still try to parse the plain text as JSON?
I searched and try a way:
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments)
But the error is:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x7ff4b1790660 {NSDebugDescription=Garbage at end.})