dongshimao7115 2016-04-13 19:25
浏览 1200
已采纳

设置跨域文件上传的响应头和请求头

I'm trying to upload a file from localhost:8888 to www.base.com . When the upload starts I have this error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote 
resource at http://base.com/public_upload. (Reason: missing token 'x-file-name' 
in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel).

Here are the header responses from php server that I set

header('Access-Control-Allow-Origin: http://localhost:8888');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

And here is the request header in upload script which runs in browser hosted by localhost:8888

xhr.setRequestHeader("Content-Type", "multipart/form-data"); 
xhr.setRequestHeader("X-File-Name", unescape(encodeURIComponent(file.name)));
xhr.setRequestHeader("X-File-Size", file.size);
xhr.setRequestHeader("X-File-Type", file.type);

What headers I've could mis-configed to create such error message?

  • 写回答

1条回答 默认 最新

  • dtkmejg127475 2016-04-13 19:30
    关注

    You have to add X-File-Name, X-File-Size and X-File-Type to the list of headers:

    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, X-File-Name, X-File-Size, X-File-Type");
    

    Removing the Access-Control-Allow-Headers header should work too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集