获取swift postvariables时出错
我在swift ios中创build了一个HTTP POST请求。 当我在PHP文件中获取一个postvariables,它显示空白,我没有得到一个variables值。 在我的swift代码下面,看看它。
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']; ?>
任何人都可以build议我该如何获得variables值。 谢谢
您应该使用application/x-www-form-urlencoded
的Content-Type
application/x-www-form-urlencoded
而不是application/json