作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到了html5上传脚本的一部分,如下:
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
alert('done');
var i =xhr.getResponseHeader('header');
alert(i.status);
}, false);
xhr.open("post", "upload/process", true);
xhr.setRequestHeader("Content-Type", "application/json-rpc");
xhr.setRequestHeader("X-File-Name", file.fileName);
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.setRequestHeader("X-File-Type", file.type);
xhr.send(file);
我的 PHP 如下:
$name = $_SERVER['HTTP_X_FILE_NAME'];
$path = '/upload/';
if(file_put_contents($path.$name, "php://input"))
{
echo json_encode(array('status'=>'sucess', 'name'=>$name));
}else{
echo json_encode(array('status'=>'error'));
}
我想从服务器获取 Json 响应并显示它。到目前为止我不知道如何,我尝试过
var i =xhr.getResponseHeader('header');
alert(i.status);
但是我收到错误“NULL”。
最佳答案
MDC - XMLHttpRequest documentation提供了很好的信息。
响应包含在xhr.responseText
中。所以你必须这样做:
xhr.addEventListener("load", function () {
alert('done');
var i = JSON.parse(xhr.responseText);
alert(i.status);
}, false);
关于php - 如何在 PHP html5 上传中获取服务器响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5078708/
我是一名优秀的程序员,十分优秀!