2017-09-13 27 views
1

我正在開發一個項目,下面是我的Swift代碼。但是當我運行時,我得到這個錯誤。錯誤域= NSCocoaErrorDomain代碼= 3840「JSON文本沒有以數組或對象和選項啓動,以允許片段未設置。」在Swift 3中

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

我也檢查從數據庫的響應它說,它有一個有效的內容長度。但由於這個錯誤,我無法得到我想要的東西。

從我的代碼中的響應打印;

Optional(<NSHTTPURLResponse: 0x608000233bc0> { URL: http://192.168.1.99/insertData.php } { status code: 200, headers { 
    Connection = "Keep-Alive"; 
    "Content-Length" = 10; 
    "Content-Type" = "text/html; charset=UTF-8"; 
    Date = "Wed, 13 Sep 2017 21:37:38 GMT"; 
    "Keep-Alive" = "timeout=5, max=100"; 
    Server = "Apache/2.4.10 (Raspbian)"; 
} })` 

這是我的代碼。

let urlOfSMARTCF = URL(string: "http://192.168.1.99/insertData.php") 
let request = NSMutableURLRequest(url: urlOfSMARTCF! as URL) request.httpMethod="POST" 
request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
request.addValue("application/json", forHTTPHeaderField: "Accept") 

for contact in contactsCaptuure 
{ 
    let userMobileNumber = DBManager.shared.retriveRegisteredNumberOfMobile()  
    let postParameters = "\"usermobilenum\":\(String(describing: userMobileNumber!)),\"contactnum\":\(contact.phoneNumber!)"; 
    request.httpBody = postParameters.data(using: String.Encoding.utf8) 
    let task = URLSession.shared.dataTask(with: request as URLRequest) 
    { 
     data, response, error in 

     if error != nil 
     { 
      print("error is \(String(describing: error))") 
      return; 
     } 
     do 
     { 
      print(response) 
      let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary 
      if let parseJSON = myJSON 
      { 
       var msg : String! 
       msg = parseJSON["message"] as! String? 
       print(msg) 

      } 
     } 
     catch 
     { 
      print(error.localizedDescription) 
      print(error) 
     } 

    } 
    print("Done") 
    task.resume() 
} 

因此,要完成我的項目,我需要解決此問題。請幫助。謝謝。

回答

0

薩姆響應是這樣的

回波( '{ 「響應」:[{ 「狀態」: 「失敗」}]}');或

echo('{「Response」:[{「status」:「success」},{「userid」:'。$ userid。'}]}');

試試這個,請讓我們知道... :)

相關問題