2

2つの配列と複数の値を送信するjsonを送信していますが、これを送信すると、成功コード200が返され、アクセスが拒否されたplsを示す応答が表示され、誰でもそれを解決する正しい方法が得られます**

-(void)SaveColumnConnection
{

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURLURLWithString:@"http://xxxyyyzzzz.php"]];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:oneRowColumn,@"oneRowCols",memberTid,@"table_title_id",totalRowStr,@"totRow",rowandColumn,@"tempColName",tableOptIdArray ,@"tempOptid",companyId,@"companyid",@"savecolumniphone",@"tag",nil];
NSLog(@"dict %@",dict);
SBJSON *parser =[[SBJSON alloc] init];
NSString *jsonString = [parser stringWithObject:dict];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]  completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];
    if (statusCode==200) {
        //Request goes in success
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Json for post array ----------%@",str);
    }
    else{
        ///request is get failed
        NSLog(@"Error Description %@",[error localizedDescription]);
    }
}];
[request release];
  }
4

2 に答える 2

1

あなたのphpコードも見たい..とにかく、次の例が役立つかどうかを確認してください...ここでは、辞書の値を送信していません。コードを要件に合わせて調整してください:)

NSString *jsonRequest = @"{\"username\":\"user\",\"password\":\"pwd\"}";
NSLog(@"Request: %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"http://xxxyyyzzzz.php"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
于 2012-12-20T11:47:27.130 に答える
0

このコードを試してください

NSDictionary *dict = [NSDictionary
                          dictionaryWithObjectsAndKeys:oneRowColumn,@"oneRowCols",memberTid,@"table_title_id",totalRowStr,@"totRow",rowandColumn,@"tempColName",tableOptIdArray ,@"tempOptid",companyId,@"companyid",@"savecolumniphone",@"tag",nil];
    NSString *jsonRequest = [dict JSONRepresentation];

    NSURL *url = [NSURL URLWithString:@"http://xxxyyyzzzz.php"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];

    connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
于 2012-12-20T13:05:42.270 に答える