2017-05-31 174 views
1
let parameters = [ 
     "checkout": [ 
      "email": "[email protected]", 
      "line_items": [ 
       "variant_id": 31342168513, 
       "quantity": 1 
       ], 
      "shipping_address": [ 
       "first_name": "John", 
       "last_name": "Smith", 
       "address1": "126 York St.", 
       "city": "Ottawa", 
       "province_code": "ON", 
       "country_code": "CA", 
       "phone": "(123)456-7890", 
       "zip": "K1N 5T5" 
      ] 
     ] 
    ] 

    let urlString = "https://\(Key):\(Password)@sapphireonline-staging.myshopify.com/admin/checkouts.json" 
    let headers: HTTPHeaders = [ 
     "X-Shopify-Storefront-Access-Token": "5681ded39ead3fa6f4594ad0981367", 
     "Content-Type": "application/json" 
    ] 

    Alamofire.request(urlString, method:.post, parameters: parameters,encoding: JSONEncoding.default, headers:headers).responseJSON { response in 
     switch response.result { 
     case .success: 

      print("\n\n Alamofire Response . resposne :- ", response) 
     case .failure(let error): 

      print("\n\n Alamofire Failure :- ",error as NSError) 
     } 
    } 

爲什麼下面的代碼給我的錯誤:Alamofire POST請求的問題

Alamofire失敗: - 錯誤域= Alamofire.AFError代碼= 4「JSON不能因爲錯誤的連載: 數據無法閱讀,因爲它的格式不正確。「

回答

2

服務器的響應不是JSON。也許你有錯誤迴應。我建議你檢查響應錯誤代碼或使用curl嘗試相同的請求。

+0

好@janusfn和謝謝。 –