ios - AFNetworking Send JSON as a parameter for POST request -
i gone several answers same question...but unable figure out issue..i getting error
* terminating app due uncaught exception 'nsinvalidargumentexception', reason: '* +[nsjsonserialization datawithjsonobject:options:error:]: invalid top-level type in json write'
my code is:
nsdictionary *logindict=[[nsdictionary alloc] initwithobjectsandkeys:password.text,@"password",username.text,@"username",nil]; nserror *error; nsdata *jsondata = [nsjsonserialization datawithjsonobject:logindict options:nsjsonwritingprettyprinted error:&error]; if (jsondata) { nsstring *jsonstring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; } else { nslog(@"got error: %@", error); } afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; manager.requestserializer = [afjsonrequestserializer serializer]; [manager.requestserializer settimeoutinterval:20]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"accept"]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"]; manager.responseserializer = [afjsonresponseserializer serializer]; manager.responseserializer = [afjsonresponseserializer serializerwithreadingoptions:nsjsonreadingallowfragments]; [manager post:baseurllogin parameters:jsonstring success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); }];
try this
directly pass logindict
in call , check
manager.requestserializer = [afjsonrequestserializer serializer]; /* [manager.requestserializer settimeoutinterval:20]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"accept"]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"]; manager.responseserializer = [afjsonresponseserializer serializer]; manager.responseserializer = [afjsonresponseserializer serializerwithreadingoptions:nsjsonreadingallowfragments]; */ [manager post:baseurllogin parameters: logindict success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); }];
Comments
Post a Comment