2014-03-03 92 views
0

我的帖子出現了一些問題,一旦我按下「發送」按鈕,我收到一封電子郵件,但它不包含我在文本字段中輸入的內容。 我剛收到一封空的電子郵件。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 
+0

第一搜索谷歌關於你的問題,因爲很多可用的答案SOF(stackoverflow) – codercat

+0

這就是我問我之前所做的。但我無法找到問題,爲什麼它不適用於此代碼。 – Benny

+0

請幫我... – Benny

回答

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

感謝嘗試