我正在尝试从php Web服务中获取数据以检查登录详细信息。但是,Objective C代码似乎没有正确提交数据...请在下面找到我的Objective C和PHP代码
- (IBAction)doLogInPressed:(UIButton *)sender {
NSString *noteDataString = [NSString stringWithFormat:@"username=%@&password=%@", self.logInUsername.text, self.logInPassword.text];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://www.simonsayssolutions.co.uk/giftlistapi/checkLogin.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[noteDataString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:dataRaw
options:kNilOptions error:&error];
NSString *status = [json objectForKey:@"id"];
if(status.integerValue > 1){
self.logInUserCentreConstraint.constant = self.view.frame.size.height;
self.user = [[glUser alloc] init];
self.user.dbId = [json objectForKey:@"id"];
self.user.username = [json objectForKey:@"username"];
self.user.password = [json objectForKey:@"password"];
self.user.forename = [json objectForKey:@"firstname"];
self.user.surname = [json objectForKey:@"surname"];
self.user.email = [json objectForKey:@"email"];
[self performSegueWithIdentifier:@"Show Main Menu" sender:self];
} else {
}
}];
[dataTask resume];
}
这是它调用的php web服务......
<?php
if (isset ($_POST["username"])){
$username = $_POST["username"];
$username = $_POST["password"];
$link = mysql_connect('www.simonsayssolutions.co.uk:3306','simonsay_root', 'PASSWORD') or die ('Cannot connect to DB');
mysql_select_db('simonsay_giftlist', $link) or die ('Cannot select db');
$query = "SELECT * FROM user where (username = '$username' AND password = '$password');";
$result = mysql_query($query, $link) or die ("Could not execute query");
$myArray = array();
header('Content-type:application/json');
while($user = mysql_fetch_array($result))
{
$myArray[] = $user;
}
echo json_encode($myArray);
} else {
echo "no data!";
}
?>
答案 0 :(得分:1)
<?php
if (isset ($_POST["username"])){
$username = $_POST["username"];
$password = $_POST["password"];
$link = mysql_connect('www.simonsayssolutions.co.uk:3306','simonsay_root', 'bradicus1') or die ('Cannot connect to DB');
mysql_select_db('simonsay_giftlist', $link) or die ('Cannot select db');
$query = "SELECT * FROM user where (username = '$username' AND password = '$password');";
$result = mysql_query($query, $link) or die ("Could not execute query");
$myArray = array();
while($user = mysql_fetch_array($result))
{
$myArray[] = $user;
}
echo json_encode($myArray);
} else {
echo "no data!";
}
?>
关于错误:
$ username = $ _POST [“password”];
头( '内容类型:应用/ JSON');
请记住,在发送任何实际输出之前必须调用header(),在这种情况下无论如何都不需要。