2017-09-05 54 views
0

我發現一些線程相同的問題,但他們的答案不適合我。Swift - Alamofire「字符0周圍的值無效」

我有一個post請求檢查用戶登錄。

URL格式:

http://support.xxx.xx:8031/serpapi/login/checkLogin

參數:

["contextInfo" : 
     ["clientId": "1000000", "orgId": "1000001", 
     "warehouseId": "1000002", "roleId": "0"], 
     "userName": "hanoiaUser", "password": "hanoiaUser"] 

請求:

Alamofire.request(url!, method: .post, parameters: params).responseJSON { (response) in 
     print(response) 
    } 

錯誤響應:

FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) 

爲什麼會出現這個錯誤?以及如何解決它?

編輯:我試圖與郵遞員,它返回的響應:

內容類型:應用程序/ JSON

身體:原料 - JSON

{ 

    "contextInfo" : { 
     "clientId":1000000, 
     "orgId": 1000001, 
     "warehouseId": 1000002, 
     "roleId": 0 
    }, 
    "userName": "hanoiaUser", 
    "password": "hanoiaUser" 
} 

RESPONSE:

{ 
     "success": true, 
     "data": [ 
      { 
       "userId": 1000003, 
       "userName": "hanoiaUser", 
       "token": "b7e804d25065e5c3ac97d765180b7986" 
      } 
     ], 
     "error": null 
    } 
+0

它與你的服務器有問題可能 – Siyavash

+2

顯然,反應不JSON有效。你可以使用'responseString'來代替並顯示那個字符串嗎? – Larme

+0

我已經嘗試過了,但它返回了另一個錯誤:「錯誤415不支持的媒體類型」 –

回答

1

設置請求頭,如: request.allHTTPHeaderFields = 「內容類型」: 「應用/ JSON」]

0

好吧,我終於解決了這個問題

這裏有新的要求:

Alamofire.request(url!, method: .post,encoding : JSONEncoding.default, headers: headers, parameters: params).responseJSON { (response) in 
    print(response) 
} 

頁眉:

let headers = [ 
     "Content-Type": "application/json" 
    ] 
相關問題