我有下面的代码运行正常,但似乎没有发布任何东西,或者至少我的PHP不接受它。我做错了什么?
这是我的php:
$usr = $_POST['username'];
$psw = $_POST['password'];
if ($usr == '1' && $psw == '1') {
echo 'Yes';
}
这是我的目标-c
NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text];
NSString *hostStr = @"http://ep.samico.dk/blogapp/login.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:@"Yes"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"success" message:@"You are authorized"
delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alertsuccess show];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Fail" message:@"Invalid Access"
delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alertsuccess show];
}
答案 0 :(得分:0)
关闭评论中所说的内容:
以下部分(您的代码)生成URL字符串,并在其末尾添加参数。
NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text];
NSString *hostStr = @"http://ep.samico.dk/blogapp/login.php?";
hostStr = [hostStr stringByAppendingString:post];
你可以修改它来实际执行POST 或你可以修改你的PHP代码来处理它们作为GET而不是POST。
$usr = $_GET['username'];
$psw = $_GET['password'];
if ($usr == '1' && $psw == '1') {
echo 'Yes';
}
答案 1 :(得分:0)
要在objc中执行POST,您需要直接使用NSURLConnection
,如下所示:
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", @"username", @"pass"];
NSString *hostStr = @"http://ep.samico.dk/blogapp/login.php?";
NSURL *hostURL = [NSURL URLWithString:hostStr];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:hostURL];
[req setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
NSString *contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];