我有这段代码可以将一些参数发送到 PHP 脚本。理论上,脚本应该返回一个 TXT 输出,如
header('Content-Type: text/plain');
for ($i = 0; $i < $number; $i++) {
echo($arrayNumbers[$i] . "\n");
}
输出类似于数字,每行一个
13244
23123
23455
... etc.
这是我拥有的 iPhone 脚本:
NSString *myRequestString = [NSString stringWithFormat:@"name=%@&phone=%@&address=%@", name, phone, address];
// encode the URL with % codes
NSString *escapedString = (NSString *)CFBridgingRelease
(CFURLCreateStringByAddingPercentEscapes( NULL,
(__bridge CFStringRef) myRequestString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
NSData *myRequestData = [NSData dataWithBytes:[escapedString UTF8String]
length:[escapedString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL URLWithString: @"https://myserver.com/myScript.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: myRequestData];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil) {
NSString *content = [NSString stringWithUTF8String:[data bytes]];
NSLog(@"responseData: %@", content);
}
else if ([data length] == 0 && error == nil)
NSLog(@"empty");
else
NSLog(@"error");
}];
结果总是空的。没有从服务器下载任何内容。
我应该检查什么?谢谢。