【发布时间】:2023-05-22 01:21:01
【问题描述】:
我有一个返回非常简单 JSON 的 API。
首先包含 api 的服务器在我的本地机器(localhost)中,在这种情况下,一切正常。
当我尝试从 iPhone 应用程序(使用 Alamofire)连接到托管 api 时,我将 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
-
有同样的问题