我有一个发送两个字符串值的函数" Name和Text"到服务器。 当发送post方法时,php文件会发送一封包含这两个值的电子邮件。
如果我只从iOS应用中发送一个值,那么一切正常,但如果我尝试发送两个或更多值,我就不会收到任何电子邮件。
我使用的代码是:
func postToServerFunction() {
var textFromapp: NSString = sendValueText.text
var nameFromapp: NSString = sendValueName.text
println("Button Pressed")
var url: NSURL = NSURL(string: "http://example.com//iOS/send_ios.php")!
var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
var bodyData = "data=" + (textFromapp as String) //data to send
var bodyName = "name=" + (nameFromapp as String) //data to send
request.HTTPMethod = "POST"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
request.HTTPBody = bodyName.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
println(response)
if let HTTPResponse = response as? NSHTTPURLResponse {
let statusCode = HTTPResponse.statusCode
if statusCode == 200 {
// Yes, Do something.
}
}
}
}
我如何告诉HTTPBody查看这两个值?
答案 0 :(得分:4)
在这里我将如何解决它:
var bodyData = "data=" + (textFromapp as String) + "&name=" + (nameFromapp as String)
欢迎任何其他建议
答案 1 :(得分:0)
看看这个answers。有趣(ctional)是最美丽的。
function encodeData(data) {
return Object.keys(data).map(function(key) {
return [key, data[key]].map(encodeURIComponent).join("=");
}).join("&");
}