2016-04-26 81 views
0

我正在使用alamofire上傳圖像。 這是註冊API,你可以用你的個人資料照片註冊(這是我必須上傳)Alamofire .responseJSON不適用於JSON響應

所以我的代碼是這樣的(我將用另一個代碼替換print(JSON);這僅僅用於測試什麼是錯的)

func makeUploadRequest(url: String?) { 

    let imageData = NSData(data: UIImageJPEGRepresentation(self.userImage.image!, 1)!) 

    Alamofire.upload(.POST, url!, headers: ["Content-Type":"application/json"], multipartFormData: { multipartFormData in 
     multipartFormData.appendBodyPart(data: imageData, name: "image_file_1") 
     }, encodingCompletion: { 
      encodingResult in 

      switch encodingResult { 
      case .Success(let upload, _, _): 
       upload.responseJSON { (JSON) in 
        print(JSON) 
       } 

      case .Failure(let encodingError): 
       //Show Alert in UI 
       print(encodingError) 
      } 
    }) 
} 

但是當我運行這段代碼,我這個消息會遇到:

FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around 
character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} 

我知道爲什麼我得到這個消息,這是因爲響應不是JSON格式。 但反應實際上是JSON

{ 
    result: "success", 
    msg: "", 
    data: {...} 
} 

當我測試用URL的API,它工作得很好。

當我用.responseString而不是.responseJSON:它說了一些關於ASP.NET

.response:

(Optional(<NSMutableURLRequest: 0x7f8353217d50> { URL: url }), 
Optional(<NSHTTPURLResponse: 0x7f8353099040> { URL: url } 
{ status code: 500, headers { 
"Cache-Control" = private; 
"Content-Length" = 5136; 
"Content-Type" = "text/html; charset=utf-8"; 
Date = "Tue, 26 Apr 2016 06:09:03 GMT"; 
Server = "Microsoft-IIS/7.5"; 
"X-AspNet-Version" = "2.0.50727"; 
"X-Powered-By" = "ASP.NET"; 
} }), Optional(<3c68746d ... 2e2d2d3e>), nil) 

任何幫助嗎?提前致謝!

+0

嘗試'.responseString'而不是'.responseJSON',您將知道發送請求時發生了什麼錯誤。 – Fennec

+0

@Fennec哦,我嘗試了.responseString&.response。我會用這些編輯我的問題 – Joy

回答

1

嘗試更換成功塊: -

case .Success(let upload, _, _): 
       upload.responseJSON { response in 
        debugPrint(response) 
        AppHelper.hideLoadingSpinner() 
        if response.result.value is NSNull 
        { 
         print("Response nil") 
        } 
        else 
        { 
         if let JSON = response.result.value { 
          var mydict = NSMutableDictionary() 
          mydict = JSON as! NSMutableDictionary 

          if (self.delegate != nil){ 
           print("response:--------------------\n %@",mydict) 
          } 
         } 
         else 
         { 
          print("response not converted to JSON") 
         } 

        } 
        upload.response(completionHandler: { (request, response, data, error) -> Void in 

          NSLog("upload.response : data : %@", String(data: data!, encoding: NSUTF8StringEncoding)!) 
          NSLog("upload.response : response : %@", response!) 


         }) 
    } 
+0

嘗試過;響應未轉換爲JSON! – Joy

+0

好吧,現在你想看到我編輯我的問題的迴應只是指。 –

+0

upload.response:response: {URL:~~ url ~~} {status code:500,headers {Cache-Control} = private; 「Content-Length」= 5136; 「Content-Type」=「text/html; charset = utf-8」; 日期=「星期二,2016年4月26日05:59:39 GMT」; Server =「Microsoft-IIS/7.5」; 「X-AspNet-Version」=「2.0.50727」; 「X-Powered-By」=「ASP.NET」; }} – Joy

0

檢查結果,如果它是獲取值前一個失敗或成功。例如:

switch response.result { 
case .Success(let value): 
    print("Value: \(value)") 
    break 
case .Failure(let error): 
    print("Error: \(error)") 
    break 
}