【问题标题】:Sending variables to php from objective C Cocoa从目标 C Cocoa 向 php 发送变量
【发布时间】:2013-12-20 09:41:48
【问题描述】:

我正在尝试将变量传递给 php 页面,但它没有传递给页面或保存在数据库中。请参阅以下代码中的 cmets:

NSString *fname=@"fname";
NSString *lname=@"lname";
NSString *urlString = [[NSString alloc] initWithFormat:@"http://localhost:8888/Test-objective%20c%20/send.php?first_name=%@&last_name=%@",fname,lname];

NSLog(urlString);

NSURL *url = [ NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSAlert *dq= [[NSAlert alloc] init];
[dq setMessageText:urlString];
[dq runModal];
[request setHTTPMethod:@"GET"];

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

NSAlert *d= [[NSAlert alloc] init];
[d setMessageText:@"connection"];
[d runModal];

//This is the part I don't understand.
//This line has a problem:  
NSString *urlString = [[NSString alloc] initWithFormat:@"http://localhost:8888/Test-objectivec/send.php?first_name=%@&last_name=%@",fname,lname];

//But when I replace it with
NSString *urlString = @"http://localhost:8888/Test-objectivec/send.php?first_name=My&last_name=nme";

//Then it works just fine.

另外,谁能告诉我如何查看服务器的响应?

【问题讨论】:

  • 将日志更改为NSLog(@"URL string: %@", urlString);。调试,一切都创建了吗?

标签: php string cocoa nsurlconnection


【解决方案1】:
NSString *fname=@"fname";
NSString *lname=@"lname";
NSString *urlString = [[NSString alloc]  

NSString *urlString = [[NSString alloc]initWithFormat:@"http://localhost:8888/Test-objectivec/send.php?first_name=%@&last_name=%@",fnamelname];
  • 从转换 %20 的“测试目标 c”中删除空格,这会造成问题。

【讨论】:

  • %20 在目标 c 中被视为特殊
  • 我已删除空格,但变量未插入数据库。当我通过变量给出值时,问题是什么
  • 所以在 % 的位置添加 %%
  • 当我使用 NSString *urlString = @"localhost:8888/Test-objectivec/…";但是当使用 NSString *urlString = [[NSString alloc]initWithFormat:@"localhost:8888/Test-objectivec/…; 它不插入。
  • 我已经添加了 //这是我不明白的问题,我不明白的主要问题。
【解决方案2】:

当您创建具有格式的字符串时,您需要注意格式中的特殊字符。特别是在您的情况下%。格式应为:

NSString *urlString = [[NSString alloc] initWithFormat:@"http://localhost:8888/Test-objective%%20c%%20/send.php?first_name=%@&last_name=%@",fname,lname];

【讨论】:

  • 当我使用 NSString *urlString = @"localhost:8888/Test-objectivec/…";但是当使用 NSString *urlString = [[NSString alloc]initWithFormat:@"localhost:8888/Test-objectivec/… 时不起作用。
  • 您评论中的链接无效。在问题底部添加代码更新(并检查格式)。
  • 我在我的问题中添加了“这是我不明白的东西”,这是我不明白的主要问题
猜你喜欢
  • 2010-12-12
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多