将Post请求发送到PHP Server
我试过用我的按钮从两个文本域发送一些数据,一旦我按下按钮,我收到一封电子邮件,没有我在iOS模拟器中输入的文本字段的内容。 我找不到问题,请帮帮我。
ViewController.h
#import @interface ViewController : UIViewController { UITextField * firstname; UITextField *lastname; UIButton *sendPost; } @property (strong, nonatomic) IBOutlet UITextField *firstname; @property (strong, nonatomic) IBOutlet UITextField *lastname; @property (strong, nonatomic) IBOutlet UIButton *postButtonTapped; -(IBAction)postButtonTapped:(id)sender; @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize firstname; @synthesize lastname; - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)postButtonTapped:(id)sender { NSString *myRequestString = [NSString stringWithFormat:@"firstname=%@&lastname=%@",firstname.text,lastname.text]; NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://textfake.com/test/mail.php"]]; [request setHTTPMethod: @"POST"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [request setHTTPBody: myRequestData]; NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil]; NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding]; NSLog(@"%@",response); } - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.firstname resignFirstResponder]; [self.lastname resignFirstResponder]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
PHP代码
AFNetworking 2.0
- (IBAction)postButtonTapped:(id)sender { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *params = @{@"firstname": firstname.text, @"lastname": lastname.text}; [manager POST:@"http://textfake.com/test/mail.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; }
希望这有效。
对于iOS部分,试试这个:
- (IBAction)postButtonTapped:(id)sender { NSURL *url = [NSURL URLWithString:@"http://textfake.com/test/mail.php"]; NSString *post = [NSString stringWithFormat:@"firstname=%@&lastname=%@",firstname.text,lastname.text]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; // set POST method [urlRequest setHTTPMethod:@"POST"]; // adding keys with values NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; [urlRequest addValue:postLength forHTTPHeaderField:@"Content-Length"]; [urlRequest setHTTPBody:postData]; NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]]; NSURLSessionDownloadTask *urlSessionDownloadTask = [urlSession downloadTaskWithRequest:urlRequest completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) { NSData *data = [NSData dataWithContentsOfURL:localfile]; //do what you want with your data here. PS your are not on main thread ! }]; [urlSessionDownloadTask resume]; }
对于PHP部分,你不能得到文本,lat,lon,因为你从来没有发布它!
我解决了这个问题,好像有一些问题:
[NSURL URLWithString: @"http://textfake.com/test/mail.php"]];
它应该与www。 代替:
[NSURL URLWithString: @"http://www.textfake.com/test/mail.php"]];
- 在animation之前改进HTML5 Canvas逐帧JPGanimation来完全caching
- types任何没有下标成员Swift 3.0
- Unity iOS构build错误:使用Firebase Unity SDK + Google VR SDK for Unity时出现重复符号
- iOS崩溃与javascript核心,网络线程有关
- 通用表视图控制器
- 根据元组的第二个值对数组进行sorting
- 在UITextField中从右向左animation文本
- 如何sorting在iOS中的NSMutableArray中的时间
- 使用UIImageViewanimation创buildstream畅的animation