我的服务器上有一个PHP脚本,我正在尝试从我的ios应用程序发布数据,但是当我打印帖子变量时,我得到一个零。知道为什么会这样吗?
斯威夫特
let url: NSURL = NSURL(string: "https://xxxxxx.com/myFile.php")!
let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
let bodyData = "username=\(username.text)&password=\(password.text)"
request.httpMethod = "POST"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main)
{
(response, data, error) in
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
//
print("response \(responseString)")
}
PHP
print_r($_POST); //prints: ([username] => xxxxxx [password] => yyyyyy)
json_encode($postVarUser);
json_encode($postVarPass);
答案 0 :(得分:1)
可能是echo的问题($ postUser); 只需使用json_encode($ postUser);