繁体   English   中英

Alamofire 请求自行更改方法名称

[英]Alamofire request changes method name on its own

我正在使用以下代码:

func readInfo()
{
    let customHeader : HTTPHeaders = [
                "X-AUTH-TOKEN" : accessToken
            ]
    let body : Parameters = [
                :
            ]
    Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 1000
    Alamofire.request(requestAddress, method: .get, parameters: body , encoding: JSONEncoding.default, headers: customHeader).responseJSON { 

    response in

     //utility code           

    }
}

当它第一次运行时它工作得很好但是当它运行不止一次时(比如少于 30 秒),我的服务器给出错误: osweb.servlet.PageNotFound : Request method 'T' not supported

我还在 Alamofire 响应中收到状态代码 405。 这是出乎意料的,因为我正在发送.get请求。 为什么会发生这种情况,我应该如何避免它? 我无法理解。

另请注意,这不是服务器错误,因为在 Postman 上运行时请求按预期工作。

尝试 Alamofire

 var parameters = Parameters()
 parameters = [
 //Your Params
 ]

 Alamofire.SessionManager.default.session.configuration.timeoutIntervalForRequest = 1000
 Alamofire.request("\(url)", method: .get, parameters: parameters, encoding: JSONEncoding.default)
  .responseJSON {
   response in switch (response.result)
    {
       case .success(let data):
          // your code for success
       break

       case .failure(let error):
           print("Server_Error",error.localizedDescription)
        break
    }

缓存或超时没有错误。 错误在于encoding 我必须将它保存到URLEncoding.httpBody以使请求按预期工作。 我仍然不明白为什么它工作一次而不是第二次工作几秒钟。 奇怪的情况,但这是解决方案。 请发表评论以帮助我和其他人了解为什么会发生这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM