0

我在这里很困惑。基本上,我有一个 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 sucessfull'。但是,在浏览器中查看时该短语不存在?

我真的很感激这方面的任何帮助!

4

1 回答 1

3

似乎很奇怪。尝试这个

if(isset($_POST['receipt'])
{
 if($_POST['receipt'] == 'This should work?')
    {
        echo "post successfull";
    }
    else
{
    echo "not post";
    }
}
?>

此外,当您检查浏览器时,是否是在您从同一浏览器发出发布请求(例如通过表单 POST)之后?

您的 iOS 代码正在发帖,这就是它成功返回的原因。如果您只是在同一设备/浏览器上加载 .php 文件而没有在该 .php 页面上进行任何类型的发布,它将显示“未发布”。

于 2013-03-27T05:48:33.657 回答