2016-07-07 135 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/

相关问题