发出POST请求时,Xcode调试器显示错误
错误域= NSCocoaErrorDomain代码= 3840“ JSON文本不是以数组或对象开头,并且没有允许设置片段的选项。” UserInfo = {NSDebugDescription = JSON文本不是以数组或对象开头,并且未设置允许片段的选项。}
这是POST函数,
func startArchive() {
let app_Id = (appId.base64Decoded()!)
let job_Id = (jobId.base64Decoded()!)
var response_ = 0
guard let url = URL(string: "https://xxx.yyyy.com/question/abc") else {return}
let request = NSMutableURLRequest(url:url)
request.httpMethod = "POST"
let postString = "session_id=\(pSessionId)&job_id=\(job_Id)&app_id=\(app_Id)&action=start"
print(postString)
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
do {
if let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject]{
print(responseJSON)
print(responseJSON["status"]!)
response_ = responseJSON["status"]! as! Int
print(response_)
//Check response from the sever
if response_ == 200
{
OperationQueue.main.addOperation {
print("Login Successful")
}
}