Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

ios - Posting data to database with Alamofire failed

I am having an issue with posting data using Alamofire. I am making a comment box. I grab user data from the server and post his comment using his information with his comment on the article id, but when I post it sends no information to the server! I see only empty data.

The user data are successfully loaded from the server and I can see it in the console using the print accountDetails but after posting nothing is shown!

Breakpoint gives valid data too!

enter image description here

My code:

class DetailsViewController: UIViewController {
    var data: JSON?
    var userData = [JSON]()
    var accountDetails = ["name": "", "email": "", "phone": ""]
    
    @IBOutlet weak var CommentTableView: UITableView!
    @IBOutlet weak var CommentTXTField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getUserData()
        print("Account:(accountDetails)")
        
        if let id = Helper.getUserId() {
            ContactBtn.isHidden = false
        } else {
            ContactBtn.isHidden = true
        }
    }
    
    @IBAction func AddCommentBTN(_ sender: Any) {
        let commentTXT = CommentTXTField.text
        print(commentTXT!)
        
        let name =   self.accountDetails["name"]
        let mobile =  self.accountDetails["phone"]
        let email =  self.accountDetails["email"]
        let articleId = data!["id"].string!
        
        API.AddComment(articleId: articleId, name: name!, email: email!, phone: mobile!, message: commentTXT!) { (error: Error?, success: Bool) in
            if success {
                print("Registerd Successfuly")
            } else {
                print("Faile To Comment")
            }
        }
    }
    
    func getUserData() {
        guard let UserId = Helper.getUserMob() else { return }
        let url = "https://site.co/apis/getprofile.php?mob=" + UserId
        AF.request(url).validate().responseJSON { [self] response in
            switch response.result
            {
            case .failure(let error):
                print(error)
            case .success(let value):
                let json = JSON(value)
                if let id = json["data"]["id"].string {
                    print("id: (id)")
                }
                self.accountDetails["name"] = json["data"]["name"].string
                self.accountDetails["email"] = json["data"]["email"].string
                self.accountDetails["phone"] = json["data"]["phone"].string
            }
        }
    }
}

API.AddComment function

    class func AddComment(articleId: String, name: String, email: String, message: String, completion: @escaping (_ error: Error?, _ success: Bool)->Void){
        let url = URLs.AddComment
       
       let parameters = [
           "article_id": articleId,
           "name": name,
           "email": email,
            "message": message
       
       ] 

        AF.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default , headers: nil)
           .validate(statusCode: 200..<300)
           .responseJSON { response in
               
               switch response.result
               {
               case .failure(let error):
                completion(error, false)
                   print(error)
               case .success(let value):
                   let json = JSON(value)
                   if let id = json["data"]["id"].string {
                       print("id: (id)")
                    completion(nil, true)

                   }
                   
               }
           
       }
        
    }
question from:https://stackoverflow.com/questions/65861535/posting-data-to-database-with-alamofire-failed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...