我试图在swift中发出POST请求并尝试发送字典。在服务器端运行一个php脚本。我得到状态200但没有得到什么PHP脚本假设返回。写的帖子请求函数是
func uploadcompletedata()
{
let request = NSMutableURLRequest(URL: NSURL(string:"http://something.php")!)
_ = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
let boundary = NSString(format: "---------------------------14737809831466499882746641449")
let contentType = NSString(format: "multipart/form-data; boundary=%@",boundary)
// println("Content Type \(contentType)")
request.addValue(contentType as String, forHTTPHeaderField: "Content-Type")
let body = NSMutableData()
let JSON = (try!(NSJSONSerialization.dataWithJSONObject(["species":Student.sharedObject().helprequested!,"genus":Student.sharedObject().helpoffered!,"family":Student.sharedObject().helprecieved!,"ord":Student.sharedObject().disastermag!,"location":Student.sharedObject().textEntered!,"latitude":Student.sharedObject().latitude,"longitude":Student.sharedObject().longtitude,"store":Student.sharedObject().locationofimage,"acc":Student.sharedObject().email!], options: NSJSONWritingOptions.PrettyPrinted)))
body.appendData(JSON)
request.HTTPBody = body
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// You can print out response object
print("******* response = \(response)")
// Print out reponse body
let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
print("****** response data = \(responseString!)")
Student.sharedObject().locationofimage = responseString!
var err: NSError?
// var json = (try!NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)) as? NSDictionary
dispatch_async(dispatch_get_main_queue(),{
});
}
task.resume()
}
我有的PHP脚本片段
if (!empty($_POST)) {
if (empty($_POST['species']) || empty($_POST['genus']) || empty($_POST['family']) ||
empty($_POST['ord']) || empty($_POST['location']) || empty($_POST['latitude']) || empty($_POST['longitude']) ||
empty($_POST['store']) || empty($_POST['acc'])
) {
// Create some data that will be the JSON response
$response["success"] = 0;
$response["message"] = "Please enter all the details";
}
else {
//this is what is getting printed
}
根据调试后得到的结果,我无法在PHP
中输入这个if条件请帮助