我正在尝试将用户名发送到服务器。这是我的目标-c代码:
NSURL *postUrl=[NSURL URLWithString:@"http://yatanadam.com/Nappsak/checkTheUser.php"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:postUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
NSString *strData=@"userName=yatanadam";
NSData* data = [strData dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
NSURLResponse *response=nil;
NSError *error=nil;
NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
if (error==nil) {
NSLog(@"%@",string);
}
特此我的PHP代码:
<?php
$userName = $_POST["userName"];
echo $userName ;
$con = mysql_connect("localhost","username","password");
if (!$con)
{
$hata= "hata";
echo hata;
die('Baglanti saglanamadi: ' . mysql_error());
}mysql_select_db("yata", $con);
$degisken= "Select * from Users where user_name=$userName";
$showresult = mysql_query($degisken);
$multi_array = array();
while($row = mysql_fetch_assoc($showresult)){
$multi_array[] = $row;}
print json_encode($multi_array);
?>}
我现在在这个问题上待了几个小时。问题是我无法从服务器得到正确的答案。我的服务器代码无法捕获我的帖子数据(用户名)。我坚持任何能帮助我的人在哪里?
答案 0 :(得分:0)
您的标题说您正在发送application/json
(内容类型),但您实际发送的数据只是一个普通字符串(“userName = yatanadam”)。
您可能想要做的是发送application/x-www-form-urlencoded
(看起来这就是您的PHP所期望的。)