【问题标题】:Posting html data from angularjs to PHP将 html 数据从 angularjs 发布到 PHP
【发布时间】:2015-11-19 16:27:56
【问题描述】:

我一直在尝试将来自 Angular 的 HTML 数据作为 JSON 对象发布到 PHPMailer 并在 PHP 上对其进行解码。但是我只能发送h1, h2.... p 元素,当我使用 div / a 等作为对象时,它会显示两个错误。 Anonymous function on postingNo 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:57948' is therefore not allowed access. PHP 文件在远程服务器上。

这是我的代码:

角度控制器

$scope.documentContents = "<h2><a href='/index.html'> Div Contents with a tag </a><h2>"; // this gives error

// $scope.documentContents = "<h2> Div Contents with a tag <h2>"; this one works fine

$scope.sendEmailWithInfo = function(){
    console.log('clicked');
    $scope.dataToSend = {
        person: $scope.Fullname,
        contents: $scope.documentContents            
    };

    console.log($scope.dataToSend.contents);

    userServices.sendEmail($scope.dataToSend).success(function(response){
        console.log(response);
    })

};

角度服务

sendEmail: function (dataToSend) {

        return $http.post('http://....remote.php', dataToSend, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        });
    }

PHP 代码

<?php
header('Access-Control-Allow-Origin: *');
require 'PHPMailer-master/PHPMailerAutoload.php';


$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$person = $request->person; //used in template
@$contents = $request->contents;



$otherContents = "<h2>This is h2 html content</h2>";
$otherPerson= "Other Person";



$mail = new PHPMailer;

$mail->isSMTP();            
                         
$mail->Host = "smtp.gmail.com";

$mail->SMTPAuth = true;                          
     
$mail->Username = "email@email.com";                 
$mail->Password = "password";                           

$mail->SMTPSecure = "tls";                           

$mail->Port = 587;


ob_start();
require("template.php");
$html = ob_get_contents();
ob_end_clean();





$mail->setFrom('email here', $person);

$mail->addReplyTo('email here', 'First Last');

$mail->addAddress('email', 'John Doe');

$mail->Subject = 'PHPMailer sendmail test';

$mail->msgHTML($html);


$mail->AltBody = 'This is a plain-text message body';

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>

【问题讨论】:

    标签: javascript php html angularjs phpmailer


    【解决方案1】:

    其中一个问题可能是您使用' - in

    <h2><a href='/index.html'> Div Contents with a tag </a><h2>
    

    干扰 JSON 引号,所以如果您尝试使用以下内容:

    $scope.documentContents = "<h2><a href='/index.html'> Div Contents with a tag </a><h2>";
     $scope.dataToSend = {
            person: $scope.Fullname,
            contents: $scope.documentContents.replace(/'/g, '&quot;');          
        };
    

    【讨论】:

    • 还是一样。无论如何,当 php 文件被移动到本地服务器时,它接受 post 但显示致命错误,无法在 php 第 3 行转换为字符串。所以 $contents 不会转换为字符串。
    • _可捕获的致命错误:stdClass 类的对象无法在C:\xampp\htdocs\docurate-xampp\pdf\template.php 中转换为字符串在第3行
      _
    • 数据作为对象传递。 $scope.dataToSend = { person: $scope.Fullname, contents: $scope.documentContents.outerHTML //outerHTML has solved the problem converting it to string. };
    猜你喜欢
    • 1970-01-01
    • 2016-12-02
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多