ios - 如何使用 Alamofire 将数据发布到服务器 API/注册用户?

标签 ios swift alamofire

我刚刚开始使用 iOS。我在将数据发布到服务器端时遇到问题。实际上,我必须使用 API“https://xyz.php”(随机)注册一个用户。

这里我使用 Alamofire 代码。

        let parameters: [String: AnyObject] = ["name" : nameTxt.text as AnyObject,
        "password" : passTxt.text as AnyObject,
        "school" : schoolTxt.text as AnyObject,
        "class" : classTxt.text as AnyObject,
        "email" : emailTxt.text as AnyObject,
        "mobileno" : mobileTxt.text as AnyObject,
    ]


    Alamofire.request("https://xyz.php", method: .post, parameters: parameters, encoding: JSONEncoding.default)
        .responseJSON { response in

            print(response)

    }

但是当我点击提交按钮时,它在成功响应中给出了错误消息。谁能帮我吗。使用户注册。使用 Alamofire 给定参数。

最佳答案

  //  Try this after your parameters, with your URL
 Alamofire.request(your URL, method: .post, parameters: parameters).responseJSON
                    {
                        response in
                        //printing response
                        print(response)

                        //getting the json value from the server
                        if let result = response.result.value {

                            //converting it as NSDictionary
                            let jsonData = result as! NSDictionary
                           UserDefaults.standard.set("password", forKey: "user_id")

                           let userName = UserDefaults.standard.string(forKey: "user_id")
                           }
                        }

               // view controller identifier for your next view controller
     let abcd = self.storyboard?.instantiateViewController(withIdentifier: "abcd") as! DetailsViewController

                self.present(abcd, animated: true)

                }

关于ios - 如何使用 Alamofire 将数据发布到服务器 API/注册用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50370648/

相关文章:

ios - 向上/向下滑动手势在 UIWebView 中不起作用

swift - 将 Parse.com 数据加载到 TableView 单元格时应用程序终止

php - Alamofire 发布请求失败错误

ios - 钛iOS : tab groups images not showing

ios - 在 TextField 中输入文本时不显示键盘

iphone - 更改 UITableview 中多个开关的值

ios - 推送新的 UIViewController 时 UISearchController 消失

ios - AlamofireJson/EVReflection - NSArray 元素无法匹配中的 Swift 数组元素类型

json - 带循环的 Alamofire 同步请求

在 iOS 自定义键盘中使用时不支持 iOS CoreBluetooth 状态