我有一個返回非常簡單的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
有同樣的問題 –