3

您好,我正在使用Alamofire,但我收到“无效的 JSON”。在响应中,我使用了以下代码-

parametersV = ["username":amrit21@yopmail.com, "password":123456]
let headers = ["Content-Type": "application/json", "x-csrf-token":""]

Alamofire.request(.POST, "https://dev.staffingevolution.com/api/user/login", parameters: parametersV, headers: headers).responseJSON { response in
  print(response.request)  // original URL request
  print(response.response) // URL response
  print(response.data)     // server data
  print(response.result)   // result of response serialization

  if let JSON = response.result.value {
    print("JSON: \(JSON)")
  }
}
4

2 回答 2

3

我解决了

        let parametersV = ["username":"amrit21@yopmail.com", "password":"123456"]

        Alamofire.request(.POST, "https://dev.staffingevolution.com/api/user/login", parameters: parametersV, encoding: .JSON)
            .responseJSON { response in

                if let JSON = response.result.value {
                    print("JSON: \(JSON)")
                }

        }

一个问题是编码你没有编码你的 JSON 请求。使用 编码:.JSON

于 2016-05-04T11:23:10.370 回答
0

Some times when in response there is not proper JSON then piece of code .responseJSON { response in throws exception and we can't see what type of response has been received. in such case we can print it to console before converting to .responseJSON { response in Below is full example

public  func deleteImage(_ photoId: Int) {

    let requestURL = URL(string: APPURL.BASE_API_URL + "postApi/deletePostPhoto")!
    let paramDict: [String: String] = ["photoId": String(photoId), "accessKey": APP_DELEGATE.loggedInUser.accessKey, "language":APP_DELEGATE.language.lowercased()]


    Alamofire.upload(
        multipartFormData: { multipartFormData in
            for (key, value) in paramDict {
                multipartFormData.append(value.data(using: .utf8)!, withName: key)
            }
    },
        usingThreshold:UInt64.init(),
        to: requestURL ,
        method:.post,
        headers:nil,
        encodingCompletion: { encodingResult in

            switch encodingResult {
            case .success(let upload, _, _):

                // this is point where we can get actual response recieved from server, it may have some html , xml or anything

                upload.responseData(completionHandler: { response in

                    print(response)
                    let responseData = String(data: response.data!, encoding: String.Encoding.utf8)
                    print("responseData=",responseData ?? "none")
                })

                // if there is proper JSON recieved it will be executed otherwise it will fall in failure
                upload.responseJSON { response in

                    if((response.result.value) != nil) {

                        let swiftyJsonVar = JSON(response.result.value!)
                        print("response JSON: ",swiftyJsonVar)


                    }
                    else {

                        let error = response.error
                        print(error?.localizedDescription ?? "")

                    }

                }

            case .failure(let encodingError):

                print(encodingError.localizedDescription)

            }
    })

}
于 2017-12-15T07:54:07.647 回答