AFNetworking帖子不工作?

时间:2014-03-03 10:19:14

标签: ios post afnetworking

我的帖子有些问题,一旦按下我的“发送”按钮,我收到一封电子邮件,但它不包含我在文本字段中输入的内容。 我只是收到一封空电子邮件。

这是我的viewController.m

#import "ViewController.h"
#import "AFNetworking.h"    
@interface ViewController ()<UITextFieldDelegate,UITextViewDelegate>

@end

@implementation ViewController
@synthesize firstname,lastname,text;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];

}

-(IBAction)send:(id)sender {

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"user[firstname]":firstname ,

                             @"user[lastname]": lastname,

                             @"user[text]":text};

    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    [manager POST:@"http://www.mytestpage.com/test/mail.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"JSON: %@", responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Error: %@", error);

    }];
  }

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [self.firstname resignFirstResponder];
    [self.lastname resignFirstResponder];
    [self.text resignFirstResponder];

}


@end

1 个答案:

答案 0 :(得分:0)

您好,您可以试试这个

NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://www.mytestpage.com/test/mail.php"]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


    NSString *usrpwdStr = [NSString stringWithFormat:@"user[firstname]=%@&user[lastname]=%@&user[text]=%@", firstname, lastname, text];
    [request setHTTPBody:[usrpwdStr dataUsingEncoding:NSUTF8StringEncoding]];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:JSON];
        NSLog(@"dictionary =%@",dict);

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"error =%@",error);
    }];
    [operation start];

由于