2017-01-16 13 views
1

POSTのユーザ名&をNSDictionaryとしてJSON形式でPOSTしようとしています。Swift JSONのユーザ名とパスワードの解析

ドメイン= NSCocoaErrorDomainコード= 3840「配列やオブジェクトおよびフラグメントが設定されていないことを可能にするオプションを指定して起動しませんでした
JSONテキスト」:私はこの次のエラーを取得しています
のUserInfo = {。NSDebugDescription = JSONテキストはフラグメントが設定されていない可能にするために、配列やオブジェクトとオプションで起動しませんでした}

私のコードは以下の通りです:

@IBAction func loginAuthentication(_ sender: UIButton) { 
    let username : NSString = NameTextField.text! as NSString 
    let password : NSString = passwordTextField.text! as NSString 

    let parameters = [ 
     "username": "\(username)", 
     "password": "\(password)" 
    ] 
    print(parameters) 

    let headers = [ 
     "content-type": "application/json", 
     "cache-control": "no-cache", 
     "postman-token": "121b2f04-d2a4-72b7-a93f-98e3383f9fa0" 
    ] 

     if let postData = (try? JSONSerialization.data(withJSONObject: parameters, options: [])) { 

     let request = NSMutableURLRequest(url: URL(string: "http://..")!, 
              cachePolicy: .useProtocolCachePolicy, 
              timeoutInterval: 10.0) 
     request.httpMethod = "POST" 
     request.allHTTPHeaderFields = headers 
     request.httpBody = postData 

     print(request.httpBody) 

     // let session = URLSession.shared 

     let task = URLSession.shared.dataTask(with: request as URLRequest) { 
      (data, response, error) -> Void in 
        if (error != nil) { 
             print("Error message \(error)") 

             } else { 
        DispatchQueue.main.async(execute: { 

             if let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? NSDictionary 
             { 
              let success = json["error"] as? Bool 

              print("Error from php\(success)") 

              let message = json["message"] as? String 
               // here you check your success code. 
               if (success == false) 
                 { 
                 print("Result1 \(message)") 
                 self.performSegue(withIdentifier: "", sender: self) 
                 } 
               else 
                 { 
                  self.dismiss(animated: true, completion: nil) 
                  print("Result2 \(message)") 
                 } 

             } 

             }) 
           } 
     } 

     task.resume() 
} 
} 
+0

http://stackoverflow.com/a/41328809/6656894私はあなたのスクリプトを試してみました、しかし、まだ、私はそれを介して取得していますこの回答@SwiftUser –

+0

@HimanshuMoradiyaを参照してください。 Xcodeが空のパケットを送信していると思います。 – SwiftUser

+0

兄が私にあなたのプロジェクトを送ってきます。私はそれをチェックします。 –

答えて

0

問題は、あなたのように私には見えます文字列が正しくシリアル化されていません。現在のjsonシリアライゼーション手法の代わりに、素早く辞書をデータに変換するだけです。

//Start with a dictionary 
let body = [ 
    "username": "bob", 
    "password": "admin" 
] 
//Serialize it.... you might want to put this in a do try catch block instead 
let jsonBody = try? JSONSerialization.data(withJSONObject: body, options: .prettyPrinted) 
//Add it to the request 
request.httpBody = jsonBody 

//make the request, etc. 
let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response, error in 
関連する問題