【发布时间】:2014-12-27 18:59:00
【问题描述】:
我正在使用这个(标准)JavaScript 函数来调用 PHP 文件并向其发送数据:
/**
* The PHP file which receives the data
*
* @type {string} The php filename
*/
const INSTALL_FILE = "install.php";
/**
* Passes roadTaxData to the php install file which could be get with the $_POST operator
*/
function passToPHP (paramName, data) {
var httpc = new XMLHttpRequest(); // simplified for clarity"
httpc.open("POST", INSTALL_FILE, true); // sending as POST
httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
/*
Test purposes
*/
httpc.onreadystatechange = function () { //Call a function when the state changes.
if (httpc.readyState == 4 && httpc.status == 200) { // complete and no errors
console.log(httpc.responseText); // some processing here, or whatever you want to do with the response
}
};
httpc.send(paramName + "=" + data);
}
在我的 PHP 文件中,我正在使用以下代码:
header('Content-Type: application/json');
$road_tax_data = json_decode($_POST['road-tax_data'], true);
return new RoadTaxDataParser($road_tax_data);
有没有办法自动实例化 RoadTaxDataParser 类?
【问题讨论】:
-
是什么让您认为 PHP 不会自动创建
RoadTaxDataParser类的实例? -
@Bjorn 之间有一个文件。
install.php和类所在的文件。 -
@Bjorn 据我了解 OPs 问题,他想省略
return new RoadTaxDataParser($road_tax_data);并根据请求进行自动加载。 -
我无法弄清楚您的想法,但您根本无法通过网络发送 PHP 对象并从 JavaScript 中使用它。您的脚本必须打印 JavaScript 可以解析的内容(可能是 JSON)。
-
@AxelAmthor 完全正确。
标签: javascript php oop