开发者

How to set JSON NSData with ASIFormDataReuest

Hi I want to pass my use开发者_运维问答rName and Password to WCF Webservice from iPhone. I'm using ASIFormDataRequest.

Here is my code

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"POST"];
    [request setPostValue:txtUsername forKey:@"userName"];
    [request setPostValue:txtPassword forKey:@"password"];
    [request setDelegate:self];
    [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"]; 
    [request startAsynchronous];

How do I pass JSON Data? I could use this... [request appendPostBody:jsonData]; // JSON as NSData

but how do I set JSON to NSData?

NSData *jsonData = @"{'userName':'john', 'password':'secret'}";

Please help me!


Supposing you want to encode the JSON in UTF-8 as stated in your header:

NSString *json = @"{'userName':'john', 'password':'secret'}";
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
[request appendPostBody:jsonData];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜