【问题标题】:extra argument in call alamofire swift 3call alamofire swift 3 中的额外参数
【发布时间】:2018-03-03 18:52:11
【问题描述】:

我正在尝试发出 alamofire 发布请求,我正在发送一个参数和一个正文,如下所示:

  static func sendFeedbackResultOldCustomer(customerId: String?,fbackAnswers:String? ,answers: String?, completion: @escaping (Bool , String?) ->() ){

        let parameters: Parameters = ["customer_id":customerId!,"customer_new":"0","x-session":getXSession()]
        request(urlString: APIStrings.feedbackSent, parameters: parameters, method: .post, headers: nil, encoding: answers, updateXsession: false) { (success, error, errorMsg, response) in
            if(success) {
                completion(true, nil)
            }
            else {
                completion(false, response?.result.error as? String)
            }
        }
    }

请求代码:

fileprivate static func request (urlString: String!, parameters: Parameters?, method: HTTPMethod, headers: HTTPHeaders?,encoding: String!, updateXsession: Bool, completion: @escaping(Bool, Error?, String?, DataResponse<Any>?) ->()) {
        Alamofire.request(urlString, method: method, parameters: parameters, headers: headers,encoding:encoding).responseJSON { (response) in// here is the error (extra argument method in call)
            let contentType = response.response?.allHeaderFields["X-Session"] as? String
            if (updateXsession)
            {
                UserDefaults.standard.set(contentType, forKey: "x-session")
            }

            let success = checkIfSuccess(response: response)
            if(success){
                completion(success, nil, nil, response)
            } else {
                completion(success, response.error, "Failed", nil)
            }
        }

在 alamofire 请求中,我收到以下错误:调用中的额外参数。知道发生了什么吗?

我发送的参数:

let parameters: Parameters = ["customer_id":customerId!,"customer_new":"0","x-session":getXSession()]

我正在尝试发送的正文:

X_types = [{"type_id":"17","value":"3"},{"type_id":"12","value":"test"},{"type_id":"14","value":"4"},{"type_id":"19","value":"3"},{"type_id":"16","value":"4"},{"type_id":"13","value":"3"},{"type_id":"18","value":"4"},{"type_id":"15","value":"4"},{"type_id":"2","value":"4"},{"type_id":"11","value":"1"},{"type_id":"1","value":"3"},{"type_id":"8","value":"3"},{"type_id":"6","value":"2"},{"type_id":"4","value":"22-09-2017 - 12:1"},{"type_id":"5","value":"Test"}]

X_types 以编码形式发送,作为字符串(x_types 为字符串类型)

【问题讨论】:

    标签: ios swift alamofire


    【解决方案1】:

    alamofire 请求的签名是:public func request(_ url: URLConvertible, method: Alamofire.HTTPMethod = default, parameters: Parameters? = default, encoding: ParameterEncoding = default, headers: HTTPHeaders? = default)。我认为您的签名不匹配。

    试试

    let request = Alamofire.request(url, method: HTTPMethod.post, parameters: params, encoding: JSONEncoding.default, headers: header)
    request.responseJSON { response in 
    
    }
    

    【讨论】:

    • 在这种情况下我可以将正文发送到哪里?如果我将编码更改为 JSONEncoding.default ??
    • 将正文作为参数传递,参数将是字典类型[String : Any]
    • 但是我有一个参数类型字典 [String: Any],我应该在参数中也发送正文吗?试过了,还是不行,应该是body里面发送的
    • 你做得对,你必须将参数传递为[String: Any]
    • 我应该如何发送正文?不能在参数中发送。
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2018-08-26
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多