0

本质上,这似乎在服务器端起作用,但是查询字典看起来像这样。

POST: QueryDict: {u'Contestant1 John Doe Contestant2 Jane Doe ': [u'']}

它将我所有的值存储为字典中没有值的键。显然这是一个新手错误,我猜这是在 iOS 方面,所以任何帮助将不胜感激。代码如下:

    NSString *queryString = [NSString stringWithFormat:@"URL HERE"];
    NSMutableURLRequest *theRequest=[NSMutableURLRequest
                                     requestWithURL:[NSURL URLWithString:
                                                     queryString]
                                     cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];
    [theRequest setHTTPMethod:@"POST"];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"Contestant1 %@ ",contestant1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Contestant2 %@ ",contestant2] dataUsingEncoding:NSUTF8StringEncoding]];
    [theRequest setHTTPBody:body];
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (con) {
        NSLog(@"success!");
    } else { 
        //something bad happened
    }

编辑: 我的问题的解决方案。

    NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:contestant1, @"contestant1",
                              contestant2, @"contestant2", nil];

    NSError *error=nil;

    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:postDict
                                                       options:NSJSONWritingPrettyPrinted error:&error];


    [theRequest setHTTPBody:jsonData];
4

0 回答 0