javascript对象的XMLHttpRequest setRequestHeader方法不起作用 [英] javascript object's XMLHttpRequest setRequestHeader method doesn't work

查看:1600
本文介绍了javascript对象的XMLHttpRequest setRequestHeader方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件上传的问题。我使用的是。我在最新的Chrome上运行该网站。你可以猜测这个网站是在localhost上运行的。我没有在php.ini文件中改变任何东西 - 一切都设置为默认。

解决方案

下划线似乎不是标题名称的有效字符。使用连字符,它会很好地工作。

  xhr.setRequestHeader(X-FILENAME,file.name); 

,您的PHP不需要更改。


I have a problem with file uploading. I'm using that very method described in THIS article. I downloaded the whole script published by the author from HERE.
The application is meant to load files through HTML5 drag&drop and then, through javascript, send them to serverside by ajax request.
Everything works fine but the problem occurs when I want to read a parameter from AJAX request header. Here's the code of PHP "file reader":

$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);

if ($fn) {
//above there is the upload of the file with file_put_contents function
//which actually works fine when I replace $fn with my own value and ommit the "if" condition

Earlier, the XMLHttpRequest.setRequestHeader method is launched to set "X_FILENAME" header. Here's the javascript:

var xhr = new XMLHttpRequest();

// start upload
xhr.open("POST", $id("upload").action, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);

And here's the proof (a crop from my Chrome's "firebug"):

When I var_dump the $fn variable in PHP it returns boolean FALSE. What is wrong?

P.S. I'm using XAMPP v1.8.1 with Apache 2.4.3 and PHP 5.4.7 on Win7 x64. I'm running the site on the latest Chrome. As you can guess the site is running on localhost. I didn't change anything in php.ini file - everything is set to default.

解决方案

Underscores don't seem to be valid characters for header names. Use Hyphens and it will work beautifully.

xhr.setRequestHeader("X-FILENAME", file.name);

and no changes are required in your PHP.

这篇关于javascript对象的XMLHttpRequest setRequestHeader方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆