2016-10-01 39 views
3

當我升級到最新的一切(阿拉莫4,斯威夫特3和XC 8)以下停止發佈參數和我沒有一個線索,爲什麼...Alamofire 4.0,斯威夫特3 post數據沒有傳遞

let params = ["stripeToken": token.tokenId, 
       "name": name, 
       "email": email 
      ] 
    Alamofire.request(requestString, method: .post, parameters: params, encoding: JSONEncoding.default) 
     .responseJSON { (response) in 
      if response.result.value is NSNull { 
       return 
      } 
+0

jsonserialization.data肯定存在問題。 – Mark

+1

你有沒有解決這個問題?我嘗試了以下建議,但仍未通過params。 – Ross

回答

1

一切正常,因爲它應該。這是一個證明這一事實的簡單例子。

func testPostingJSON() { 
    let urlString = "https://httpbin.org/post" 

    let params: Parameters = [ 
     "stripeToken": "token_id", 
     "name": "cnoon", 
     "email": "[email protected]" 
    ] 

    let expectation = self.expectation(description: "should work") 

    Alamofire.request(urlString, method: .post, parameters: params, encoding: JSONEncoding.default) 
     .responseJSON { response in 
      if let json = response.result.value { 
       print("JSON: \(json)") 
      } else { 
       print("Did not receive json") 
      } 

      expectation.fulfill() 
     } 

    waitForExpectations(timeout: 5.0, handler: nil) 
} 

希望這個例子可以幫助您找出問題所在。乾杯。

+0

您的代碼不會測試任何內容......它只是說明.responseJSON不會返回錯誤,它不會顯示參數已發送到服務器。 – Mark

+0

我已經遍歷所有代碼,並且JSONSerialization.data存在問題,它返回傳遞給它的參數的大小(以字節爲單位),這實際上意味着沒有參數已發送到服務器。 – Mark

+0

作爲結果.responseJSON返回沒有錯誤,但服務器不接收服務器端處理的參數。我通過使用URL編碼糾正。 – Mark

13

我有一個類似的問題,我改變了從編碼到JSONEncoding.default URLEncoding.httpbody

Alamofire.request(URL, method: .post, parameters: params, encoding: URLEncoding.httpBody).responseJSON { response in 

    if let data = response.data { 
     let json = String(data: data, encoding: String.Encoding.utf8) 
     print("Response: \(json)") 
    } 
} 
6

我有同樣的問題,最後固定它。 URLEncoding.httpBody不適合我...但URLEncoding.default確實。

因此,我改變JSONEncoding.defaultURLEncoding.default

它現在將參數傳遞給後端。

Alamofire.request(loginURL, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil)