我很困惑。基本上,我有一个iOS应用程序,它将字符串上传到我的蓝色主机服务器上的。php文件。
-(IBAction)plus{
if (counter>=0 && counter<240) {
counter=counter+1;
count.text = [NSString stringWithFormat:@"%i",counter];
NSString* myString = [NSString stringWithFormat:@"%d",counter];
NSString *receipt1 = @"This should work?";
NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL
URLWithString:@"http://www.ryanmediaworks.com/validation.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-
Type"];
[request setHTTPBody:postData];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 600)
{
NSLog(@"Response: %@", result);
}
}
}
这是我的。php文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>I hope this works!</title>
</head>
<body>
<?php
if(_POST)
{
if($_POST['receipt'] == 'This should work?')
{
echo "post successfull";
}
else
{
echo "not post";
}
}
?>
</body>
</html>
这里是奇怪的部分…这是我的浏览器:
<head>
<title>I hope this works!</title>
</head>
<body>
not post</body>
</html>
但是这里是Xcode的输出:
<head>
<title>I hope this works!</title>
</head>
<body>
post successfull</body>
</html>
似乎'if'语句在xcode上工作,但不在浏览器上?换句话说,正确的字符串被上传到.php,因此返回短语"post successfully"。但是,在浏览器中查看时,该短语不存在?
我真的很感谢任何帮助在这个问题上!似乎很奇怪。试试这个
if(isset($_POST['receipt'])
{
if($_POST['receipt'] == 'This should work?')
{
echo "post successfull";
}
else
{
echo "not post";
}
}
?>
也当你检查你的浏览器,是你做一个post请求后,从同一浏览器(例如,通过表单post)?
你的iOS代码正在做一个post,这就是它返回成功的原因。如果你只是加载。php文件而没有在相同的设备/浏览器上做任何类型的post到。php页面,它会显示"not post"