首页 新闻 会员 周边 捐助

ajax跨域上传文件加了xhr报不支持options错误

0
悬赏园豆:10 [已解决问题] 解决于 2017-12-19 22:38

用ajax跨域上传文件不加xhr就能请求成功,为了实现进度条功能在ajax里面加了xhr就报错了

 

$.ajax({

url: "http://localhost:823/api/FileStream/Upload",

type: "post",

contentType: false,

cache: false,

processData: false,

contentType: false,

data: formdata,

xhr:function(){ var myXhr = $.ajaxSettings.xhr(); if (myXhr.upload) { myXhr.upload.addEventListener("progress", progressHandlingFunction,false); } return myXhr; },

success: function (text){ if (text == "success") { alert("上传成功"); }

bokeyuan_number_1的主页 bokeyuan_number_1 | 初学一级 | 园豆:83
提问于:2017-12-16 15:55
< >
分享
最佳答案
0

报什么错误?

收获园豆:10
dudu | 高人七级 |园豆:30734 | 2017-12-16 16:18

报不支持options错误 500

bokeyuan_number_1 | 园豆:83 (初学一级) | 2017-12-16 16:29

@bokeyuan_number_1: 那应该是服务端的问题

dudu | 园豆:30734 (高人七级) | 2017-12-16 16:36

@dudu: 服务端没问题,我在配置文件里面配置了

<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Max-Age" value="30"/>
<add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>

没有加xhr获取上传进度时请求一直没问题文件也上传到了服务端,加了之后就在前台ajax那个地方报错,不知是什么原因

bokeyuan_number_1 | 园豆:83 (初学一级) | 2017-12-16 16:44

@bokeyuan_number_1: 出错时的request headers与response headers是什么?

dudu | 园豆:30734 (高人七级) | 2017-12-16 17:22

@dudu: response headers :

  1. Access-Control-Allow-Headers:
    Content-Type, Accept
  2. Access-Control-Allow-Methods:
    GET,POST,OPTIONS
  3. Access-Control-Allow-Origin:
    *
  4. Access-Control-Max-Age:
    30
  5. Cache-Control:
    no-cache
  6. Content-Length:
    66
  7. Content-Type:
    application/json; charset=utf-8
  8. Date:
    Mon, 18 Dec 2017 01:29:18 GMT
  9. Expires:
    -1
  10. Pragma:
    no-cache
  11. Server:
    Microsoft-IIS/8.0
  12. X-AspNet-Version:
    4.0.30319
  13. X-Powered-By:
    ASP.NET
  14. X-SourceFiles:
    =?UTF-8?B?SDpcTXZjQXBwbGljYXRpb24xIC0g5Ymv5pysXFdlYiBBcGlcYXBpXE5ld0ZpbGVTdHJlYW1cU2F2ZUZpbGU=?=

 

request headers:

  1. Accept:
    */*
  2. Accept-Encoding:
    gzip, deflate, sdch, br
  3. Accept-Language:
    zh-CN,zh;q=0.8
  4. Access-Control-Request-Headers:
     
  5. Access-Control-Request-Method:
    POST
  6. Connection:
    keep-alive
  7. Host:
    localhost:53476
  8. Origin:
    http://localhost:63695
  9. Referer:
    http://localhost:63695/
  10. User-Agent:
    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

后来把服务端的端口改成63695了,您帮我看下,实在找不出原因了

bokeyuan_number_1 | 园豆:83 (初学一级) | 2017-12-18 09:39

@bokeyuan_number_1: 可能是Access-Control-Allow-Origin:*引起的,建议将*改为http://localhost:63695试试

dudu | 园豆:30734 (高人七级) | 2017-12-18 21:35

@dudu: 终于解决了,要在web api里面的Global写自定义header时进行处理。虽然这个答案没有解决这个问题,但给我提供了一个很好的解决思路。谢谢你啦!

bokeyuan_number_1 | 园豆:83 (初学一级) | 2017-12-19 22:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册