PHP POST+;iOS快速错误
我的应用程序有问题,我试图做的是通过“NSMutableURLRequest”URL将一些信息从我的文本字段发送到我拥有的PHP文件 错误表明我的每个变量都有未定义的索引 这是我第一次做这种练习,所以我不知道错误是在我的PHP代码中还是在Swift函数中 下面是我的swift功能PHP POST+;iOS快速错误,php,ios,swift,Php,Ios,Swift,我的应用程序有问题,我试图做的是通过“NSMutableURLRequest”URL将一些信息从我的文本字段发送到我拥有的PHP文件 错误表明我的每个变量都有未定义的索引 这是我第一次做这种练习,所以我不知道错误是在我的PHP代码中还是在Swift函数中 下面是我的swift功能 @IBAction func enviarInfo(_ sender: Any) { let request = NSMutableURLRequest(url: NSURL(string: "
@IBAction func enviarInfo(_ sender: Any) {
let request = NSMutableURLRequest(url: NSURL(string: "http://www.mydomain/index.php")! as URL)
request.httpMethod = "POST"
//The String with the vars that will be sent to the $_POST["var"]
let postString = "nombre = \(nombreText.text!) &aPaterno = \(apaternoText.text!) &aMaterno = \(amaternoText.text!) &genero = \(genero.text!) &email = \(emailText.text!) &telefono = \(telefonoText.text!)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
print("response = \(String(describing: response))")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}
PHP代码如下所示
$nombre = $_POST["nombre"];
//All the $vars look like this.
echo $nombre;
非常感谢您的帮助:)您需要从
postString
中删除空格
更改:
let postString = "nombre = \(nombreText.text!) &aPaterno = ..."
到
当发送这样的数据时,这些额外的空格在解析字符串时很重要。您是否尝试过从
postString
中删除空格nombre=\(…)
?你也可以做一个var\u dump($\u POST)
来看看你到底得到了什么。谢谢Magnus Ericsson!成功了!没想到字符串会受到空格的影响
let postString = "nombre=\(nombreText.text!)&aPaterno=..." ...and so on