AlamoFire POST工作但返回NSURLErrorDomain错误

时间:2017-12-21 22:12:30

标签: swift http-post alamofire

我正在使用AlamoFire将JSON发布到我的网络服务,该帖子正在成功运行并且值已提交到数据库,但AlamoFire正在返回NSURLErrorDomain,然后显示我的提交失败'信息。我在下面的代码中做错了什么?

//AlamoFire POST
    var request = URLRequest(url: NSURL.init(string: URL)! as URL)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.timeoutInterval = 10 // secs
    request.httpBody = try! JSONSerialization.data(withJSONObject: myJson, options: [])

    Alamofire.request(request).responseJSON {
        (response) in
        log.info("HTTP RESPONSE: \(response.result)")
        if response.result.isSuccess {
            self.alertControllerMsg(msgStyle: UIAlertControllerStyle.alert,msgTitle: "Success", msgBody: "Update Delivered", cancelLbl: "", actionLbl: "Dismiss", complete: {
                self.view.endEditing(true)                    
                self.activityIndicator.stopAnimating()
                self.sendBtn.isEnabled = true
            })
        } else if response.result.isFailure {
            let error : Error = response.result.error!
            log.error(error)
            self.alertControllerMsg(msgStyle: UIAlertControllerStyle.alert,msgTitle: "Error", msgBody: "Update Failed. Please try again.", cancelLbl: "", actionLbl: "Dismiss", complete: {
                self.activityIndicator.stopAnimating()
                self.sendBtn.isEnabled = true
            })
        }
    }

编辑:以下是完整的错误消息:

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x170643540 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://{{URL.REDACTED}}/odata/Updates, NSErrorFailingURLKey=http://{{URL.REDACTED}}/odata/Updates, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

1 个答案:

答案 0 :(得分:0)

我发现问题实际上是在服务器端。除了HTTP BODY之外,Web服务还在接收URL参数。 Alamofire不喜欢我将此参数附加到URL上。它必须是如何编码的,因为类似的调用在Fiddler中起作用,但不是通过Alamofire起作用。我计划重新使用Web服务来删除查询字符串参数。