我正在尝试使用 AFNetworking 向我的一个页面(php 页面)发送一些键值。我正在使用 AFHTTPRequestOperationManager 进行此操作,发布成功,但是我在服务器端接收到空白值。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"name":@"1234", @"email":@"1234",@"regId":@"rty2345",@"device_flag":@"2"};
NSLog(@"%@", params);
manager.responseSerializer = [AFJSONResponseSerializer serializer]; // if response JSON format
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:@"http://xyz.in/app/apns/register.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self databaseInsertion];
ShowViewController *mapp = [[self storyboard]instantiateViewControllerWithIdentifier:@"showview"];
[self.navigationController pushViewController:mapp animated:YES];
NSLog(@"%@", responseObject);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}];
我正在发回一个 JSON 字符串,以便执行成功块。但是当我尝试在服务器端获取值时,所有值都显示为空白。
然后我尝试使用 NSMutableURLRequest *request 发送数据,这也产生了相同的结果,当我尝试使用浏览器发送数据时,例如http://www.xyz.in/app/apns/register.php?name=15&email= 1234®Id=r5678d&device_flag=2,效果很好。
我使用 NSMutableURLRequest 的代码如下
NSString *post =[[NSString alloc] initWithFormat:@"name=%d&email=%d®Id=%@&device_flag=%d", status,[_emailTextField.text intValue],@"r5678d",2];
NSLog(@"%@",post);
NSURL *url=[NSURL URLWithString:@"http://xyz.in/app/apns/register.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];