我在swift ios中创建了一个HTTP POST请求。当我在php文件中获取post变量时,它显示为空白,我没有得到变量值。在我的快捷代码下面,看看吧。
var dataString = "name=john&type=student"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: "http://www.example.com/example.php")
var postString = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPBody = postString
var connection = NSURLConnection(request: request, delegate: self, startImmediately: false)
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
//New request so we need to clear the data object
println(response)
}
Php代码:
<?php
echo $_POST['name'];
?>
任何人都可以建议我应该怎样做才能获得变量值。感谢
答案 0 :(得分:0)
您应使用Content-Type
application/x-www-form-urlencoded
代替application/json