gpt4 book ai didi

php - 如何在 PHP html5 上传中获取服务器响应

转载 作者:行者123 更新时间:2023-12-02 20:24:21 24 4
gpt4 key购买 nike

我得到了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com