【问题标题】:AFNetworking post not working?AFNetworking 帖子不起作用?
【发布时间】:2014-03-03 10:19:14
【问题描述】:

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

这是我的 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

【问题讨论】:

  • 首先在谷歌上搜索你的问题,因为 SOF(stackoverflow) 中有很多答案
  • 这就是我在问之前所做的。但我找不到为什么它不适用于此代码的问题。
  • 你的 json 响应中发生了什么
  • 我收到一封电子邮件-但它没有任何内容,不知道我的参数是否有问题
  • 可能是它从您的服务器返回错误的 json 确实检查过。

标签: ios post afnetworking


【解决方案1】:

你好,你可以试试这个

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

谢谢

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多