解析错误:无法读取数据,因为格式不正确。 - Swift 3.0中的PHPMailer

时间:2017-04-08 05:02:48

标签: php json swift3 xcode8 phpmailer

我有这些swift 3.0代码将JSON数据发布到php mysql数据库中。

test.swift

let url             = URL(string: "http://localhost:test.php")
let session         = URLSession.shared
let request         = NSMutableURLRequest(url: url! as URL)
request.httpMethod  = "POST"
let valueToSend     = "data1=&data2"
request.httpBody    = valueToSend.data(using: String.Encoding.utf8)
let myAlert         = UIAlertController(title: "Confirm", message: "are you sure ?", preferredStyle: UIAlertControllerStyle.alert)
let cancel          = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)
let okaction        = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler:
    {
        action in

            let task = session.dataTask(with: request as URLRequest, completionHandler: {
                (data, response, error) in
                if error != nil {
                    return
                }
                else {
                    do {
                        if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
                        {
                            DispatchQueue.main.async {
                                let message = Int(json["message"]!)
                                if(message == 1)
                                {
                                    let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
                                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                                    let navigationController = UINavigationController.init(rootViewController: myViewController)
                                    appDelegate.window?.rootViewController = navigationController
                                    appDelegate.window?.makeKeyAndVisible()

                                    let myAlert = UIAlertController(title: "Success !", message: "sent !", preferredStyle: UIAlertControllerStyle.alert)
                                    myAlert.addAction(UIAlertAction(title: "Noted", style: UIAlertActionStyle.default, handler: nil))
                                    navigationController.present(myAlert, animated: true, completion: nil)
                                    return
                                }
                                else { return }
                            }
                        }
                    }
                    catch let parseError { print("Parse error: \(parseError.localizedDescription)") }
        }
    })
    task.resume()

    }
    )
myAlert.addAction(okaction)
myAlert.addAction(cancel)
self.present(myAlert, animated: true, completion: nil)

到目前为止,我设法将数据发送到数据库并且电子邮件工作正常。我可以使用PHPMailer收到电子邮件。但是问题发生在x代码中无法读取PHPMailer代码。

由于PHPMailer,在我的控制台中显示错误。当我删除PHPMailer代码时,它工作正常。

  

"解析错误:无法读取数据,因为它不在   格式正确。"

我的目标是,我不希望出现此错误,因为UIAlertAction会停留在同一页面上。由于该错误,它不会进入rootController。

有没有办法解决这个问题?顺便说一句,这是我的PHP代码

test.php的

<?php
    require 'database/connect.php';
    global $connect;
    require_once 'PHPMailer-master/PHPMailerAutoload.php';

    if (isset($_POST['data1'])) {
        $data1      = $_POST['data1'];
        $sql        = "SELECT * FROM table WHERE data1 = '$data1'";
        $result     = mysqli_query($connect, $sql);
        if ($result && mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result))
            {}

            $output = array('message' => '1');
            echo json_encode($output);

            $add = "INSERT INTO table (data1) VALUES ('$data1')";
            $run = mysqli_query($connect,$add);

            $mail = new PHPMailer;                              
            $mail->isSMTP();                                       
            $mail->Host = 'smtp.gmail.com';  
            $mail->SMTPAuth = true;                                
            $mail->Username = 'test@gmail.com';               
            $mail->Password = '*****';                            
            $mail->SMTPSecure = 'tls';                            
            $mail->Port = 587;                                     
            $mail->setFrom('email@email.com.my', 'Mailer');  
            $mail->addAddress('test@email','Receiver');   
            $mail->isHTML(true);                                  
            $mail->Subject = 'Test';
            $mail->Body    = 'Test';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
            if(!$mail->send()) 
            {
                    echo 'Message could not be sent.';
                    echo 'Mailer Error: ' . $mail->ErrorInfo;
            } 
            else 
            {
                    echo 'Message has been sent';
            }            
            exit();

        mysqli_free_result($result);
        }
        else 
        {}
    }
?>

感谢。

0 个答案:

没有答案