SWIFT代码:
func set(data:[AirFreightModel],userid:String,username:String,completion:@escaping(Result<String,CustomError>)->Void)
{
let url = URL(string: "\(CS.ServerName)/AirFreight/SetAirFreightApproval?UserID=\(userid)&UserName=\(username)")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
//request.addValue("application/json", forHTTPHeaderField: "Content-Type")
do{
let jsonData = try! JSONEncoder().encode(data)
let jsonString = String(data: jsonData, encoding: .utf8)!
print(jsonString)
let parameters: [String: String] = [
"AirFreight": jsonString
]
//request.httpBody = parameters.percentEncoded()
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions())
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data,
let response = response as? HTTPURLResponse,
error == nil else { // check for fundamental networking error
print("error", error ?? "Unknown error")
return
}
guard (200 ... 299) ~= response.statusCode else { // check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
do{
let decoder=JSONDecoder()
let userResponse=try decoder.decode(String.self, from: data)
completion(.success(userResponse))
}catch
{
print("Unexpected error: \(error)")
completion(.failure(.canNotProcessData))
}
}
task.resume()
}
catch
{
}
}
网页 API 代码:
public string SetAirFreightApproval(List<AirFreightModel> AirFreight,string UserID,String UserName)
{
return "1";
}
对 web api 的请求非常完美,因此 QueryString 变量数据传递工作正常,但对象数据数组无法传递。我是 Swift 的新手,所以不知道我在搞什么鬼。我正在使用 Swift 4.2。和 xcode 10. 示例 JSON 字符串:
[
{
"CauseID": " Matha Nosto Man",
"CompanyName": "",
"RaciActionList": [
],
"PoType": "",
"CauseDescription": "",
"FBID": "Test",
"ResponseResult": "",
"RACI": "",
"Destination": "",
"Remarks": "",
"Sequence": "",
"Type": "",
"BillDate": "",
"FreightType": "",
"BillAmount": "",
"BillRef": "",
"CommandName": "",
"IsChecked": false,
"EmployeeCode": "",
"BuyerORExporter": "",
"Mode": "",
"Forwarder": "",
"RunningFor": ""
}
]
哪里显示了对 web api 的请求已经完成,因为其余参数获取所有值都接受列表之一。