how to get server response in PHP html5 upload
I got a part of html5 upload script, it is as follow:
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
alert('done');
var i =xhr.getResponseHeader('header');
alert(i.status);
开发者_JAVA百科 }, 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);
And My PHP is as follow:
$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'));
}
I want to grab the Json response from server and show it. So far I don't know how, I tried
var i =xhr.getResponseHeader('header');
alert(i.status);
But i got a error "NULL".
The MDC - XMLHttpRequest documentation provides good information.
The response is contained in xhr.responseText
. So you have to do:
xhr.addEventListener("load", function () {
alert('done');
var i = JSON.parse(xhr.responseText);
alert(i.status);
}, false);
I think you might need quotes around "upload/process" in the line:
xhr.open("post", upload/process, true);
That is sort of a guess though.
精彩评论