iOS应用调用重定向域

时间:2017-04-18 17:41:51

标签: ios json web

我正在创建基于休息的应用,当我向服务器网址发出请求时: 示例:107.XXX.XXX.XXX:8080 / taxi - 它正常工作,返回给我JSON。 但是,当我向域转发请求转发到该IP时,我的应用程序向我显示异常:

2017-04-18 20:23:53.063 Project X[4121:301275] http://107.XXX.XXX.XXX:8080/taxi  
2017-04-18 20:23:53.065 Project X[4121:301275] fireGetWebserviceCall finally  
2017-04-18 20:23:53.252 Project X[4121:301275] Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo={NSUnderlyingError=0x600000240600 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response= { URL: http://107.XXX.XXX.XXX:8080/taxi } { status code: 404, headers {
    "Content-Language" = en;
    "Content-Length" = 977;
    "Content-Type" = "text/html;charset=utf-8";
    Date = "Tue, 18 Apr 2017 17:23:51 GMT";
    Server = "Apache-Coyote/1.1";
} }

当我将该URL粘贴到浏览器中时,我得到了正常的JSON响应。 有人可以向我解释为什么返回的信息是text / html以及如何修复它?

1 个答案:

答案 0 :(得分:1)

我使用下面的代码确保从网址获取JSON响应:

<强>夫特

        var request = URLRequest(url: self.url!)
        request.httpMethod = self.httpMethod
        request.httpBody = body
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")