I am sending dictionary in json and its have two array and multiple value but i am getting response access denied

元气小坏坏 提交于 2019-12-11 02:29:40

问题


I am sending two array and multiple value throw json but when i send this i am getting success code 200 and its response showing access denied pls any one give me right way to solve it **

-(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];
  }

回答1:


try this code

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]; 



回答2:


Wanted to see your php code as well..Anyways please check whether the following example helps you out... Here I am not sending any dictionary values, adjust the code to your requirement :)

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];


来源:https://stackoverflow.com/questions/13971265/i-am-sending-dictionary-in-json-and-its-have-two-array-and-multiple-value-but-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!