2016-07-07 131 views
0

我有一個返回非常簡單的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一起工作?

原諒我,如果英語是不正確的

+0

我認爲你的郵遞員參數關鍵的是不一樣的請求鍵 –

+0

關鍵的是相同的,但是如果關鍵的是不同的內容類型應該是JSON @AkshanshThakur – dieroste

+0

有同樣的問題 –

回答

-1

是否response.request符合您在郵差試過的網址是什麼? 你試過Alamofire.request(.GET,url)而不是.POST嗎? 我在一個項目中遇到了一些問題,所以我改變了我的網址,並且使用了Alamofire而沒有參數,但是使用了格式化的url。

+0

是的,是相同的URL。當我使用.GET而不是.POST時,狀態碼是403而不是405,當我使用POST時 – dieroste

0

嘗試添加斜線到您的網址:

https://domain.com/api/search/

相關問題