无法使用swift中的POST方法在api中保存数据

时间:2017-11-08 08:17:45

标签: swift3

我无法按API中的要求查看更改,当我使用Post方法时,我可以获得正确的解决方案。这是我写的代码

@IBAction func savebutton(_ sender: Any) {
        if let id1 = UserDefaults.standard.string(forKey: "Userid"),let usertype1 = UserDefaults.standard.string(forKey: "Usertype")
        {
        var request = URLRequest(url: URL(string: "http://www.shreetechnosolution.com/funded/donorprofile.php")!)
        request.httpMethod = "POST"
        let postString = "Name=\(textname.text!)&Gender=\(textGender.text!)&Email=\(textemail.text!)&MobileNo=\(textmb.text!)&Country=\(textcountry.text!)&donorid=\(id1)&usertype=\(usertype1)"
        request.httpBody = postString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {                                                 // check for fundamental networking error
                print("error=\(String(describing: error))")
                return
            }

            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(String(describing: response))")
            }

            let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
            let msg = json.value(forKey: "message") as! NSString!

            let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
            alert.show()

            }
        }

    }

有人可以帮助我吗?

0 个答案:

没有答案