Angular - 使用JS生成HTML文件并发送到服务器

时间:2014-01-04 05:31:21

标签: javascript php angularjs angular-http

我的目标是在javascript中生成一个html文件(基于表单输入),并使用Angular的$ http将此生成的文件发送到服务器(php)进行存储。这可能吗?

到目前为止,我已尝试在客户端生成HTML字符串并发送字符串,并使用php的file_put_contents()生成html文件。但我想在客户端生成html文件。随着html变得越来越复杂,我不想发送冗长而复杂的字符串。

Javascript(在控制器内)

$http({
    url: "/test.php",
    method: "POST",
    data: {"content":"<h1>hello world</h1>"}
});

test.php的

<?php
    $input = file_get_contents("php://input");
    $data = json_decode($input);
    file_put_contents("index.html", $data->content);
?>

我更喜欢在客户端生成html文件,因为我想减少服务器负载。

感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

您可以使用Twig进行PHP模板化。或PHP版 - http://platesphp.com/

从PHP读取POSTed数据并使用模板生成。

伪码:

<?php
    $input = $_POST['content'];
    $html = generateTemplate($input);
    file_put_contents($fileName, $html);
?>