javascript - 从 php 创建 XMLHTTPRequest
问题描述
我正在设置一个小网络服务(本地 LAN)来使用 ePOS 在爱普生热敏打印机上打印一些数据。他们有关于如何从 JavaScript 发送 XML 请求的很好的文档,但我的问题是我希望服务器打印数据库中的数据,所以没有 javascript。
我需要将此 JavaScript“翻译”为 phpreq
请求在哪里:
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
xhr.setRequestHeader('If-Modified-Since', 'Thu, 01 Jan 1970 00:00:00 GMT');
xhr.setRequestHeader('SOAPAction', '""');
xhr.onreadystatechange = function () {
// Receive response document
if (xhr.readyState == 4) {
// Parse response document
if (xhr.status == 200) {
alert(xhr.responseXML.getElementsByTagName('response') [0].getAttribute('success'));
}
else {
alert('Network error occured.');
}
}
};
xhr.send(req);
我已经尝试过这个:
$opts = array('http' =>
array(
'method' => 'POST',
'header' => ["Content-type: text/xml; charset=utf-8", "If-Modified-Since: Thu, 01 Jan 1970 00:00:00 GMT", "SOAPAction: ''"],
'content' => $content
)
);
# Create the context
$context = stream_context_create($opts);
# Get the response (you can use this for GET)
$result = file_get_contents($url, false, $context);
但似乎不起作用。
我将尝试使用curl,但不知何故我不明白如何正确设置标题以及如何发送数据......
有人能帮我吗?
解决方案
推荐阅读
- c# - 如何使用查询字符串从 ActionFilterAttribute 请求重定向
- python - 融合两个数组
- webhooks - 在 Nextcloud 中搜索文件
- python - 数据库 PostgreSQL 中的访问错误
- craftcms - 如何渲染在 Craft 2 CMS 中上传的图像?
- python - Pandas 不会在我的逗号分隔的 .txt 文件中分隔列
- c# - .NET 中 NaN 数的错误 Math.Pow() 实现
- ember.js - Ember.Logger.log 已弃用,console.info 给出“无控制台”构建警告,该怎么办?
- c# - C#摘要授权,但在邮递员上工作
- javascript - 防止用户使用检查元素启用按钮