dtz63853 2017-04-08 05:02
浏览 228

解析错误:无法读取数据,因为格式不正确。 - Swift 3.0中的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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python爬取指定微博话题下的内容,保存为txt
    • ¥15 vue2登录调用后端接口如何实现
    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?