我有一个返回非常简单的JSON的api。在Postman中请求REST POST API工作,但在Alamofire中不起作用
首先包含api的服务器在我的本地机器(本地主机)中,在这种情况下,一切正常。
我通过了API的虚拟主机,当我试图从iPhone应用程序(使用Alamofire)的托管API连接不工作
Alamofire.request(.POST, "https://domain.com/api/search", parameters: ["ubicacion": ubicacionId, "fecha":"\(dateFormatter.stringFromDate(fecha))"], encoding: .URL)
.responseJSON { response in
switch response.result {
case .Success:
print(response.request) // original URL request
print(response.response) // URL response
print(response.result) // result of response serialization
self.JSON = response.result.value!
//llamar el delegate
self.delegate.devolverjson(self.JSON)
case .Failure(let error):
print(response.request) // original URL request
print(response.response) // URL response
print(response.result) // result of response serialization
self.JSON = ["error":error]
//llamar el delegate
self.delegate.devolvererror(self.JSON)
}
}
在这种情况下,response.response
是:
{ URL: https://domain.com/api/search } { status code: 405, headers {
"Cache-Control" = "no-cache, private";
Connection = "Keep-Alive";
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 07 Jul 2016 18:24:26 GMT";
"Keep-Alive" = "timeout=3, max=30";
Server = "Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "PHP/5.6.22";
allow = POST;
} })
的response.result
是.Failure
和错误是
error: {
error = "Error Domain=NSCocoaErrorDomain Code=3840 \"Invalid value around character 0.\" UserInfo={NSDebugDescription=Invalid value around character 0.}";
}
当我连接到Postman的api(相同的URL:https://domain.com/api/search)时,api会返回正确的信息(JSON),请使用此标头。
Cache-Control →no-cache
Connection →Keep-Alive
Content-Type →application/json
Date →Thu, 07 Jul 2016 18:23:38 GMT
Keep-Alive →timeout=3, max=30
Server →Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9
Transfer-Encoding →chunked
X-Powered-By →PHP/5.6.22
为什么如果API使用Postman,不要与Alamofire一起工作?
原谅我,如果英语是不正确的
我认为你的邮递员参数关键的是不一样的请求键 –
关键的是相同的,但是如果关键的是不同的内容类型应该是JSON @AkshanshThakur – dieroste
有同样的问题 –