ajax跨域上传文件

很多用到AJAX请求,如何区别一个请求是传统的HTTP请求?还是Ajax请求?网上有很多,这里记录一下,方便以后直接查看,大笑懒人计划,

关键是看http请求头(header)信息中的 x-requested-with 信息项是否有无,如果无该头信息项,取出的是未定义的值,即null,则说明是传统请求;如果存在该头信息项,值是XMLHttpRequest 则说明是Ajax请求;

有时候做ajax上传的时候 ,会报错误。blob.png

x-requested-with  不允许,然后就是需要在xhr对象那里 那个头声明给去掉,或者在服务端添加对它的支持 如 header('Access-Control-Allow-Headers:x-requested-with,content-type'); 

原理呢 上一部分 源代码

  var xhr = false;
  try{
    xhr=new XMLHttpRequest();//尝试创建 XMLHttpRequest 对象,除 IE 外的浏览器都支持这个方法。
  }catch(e){
   xhr=ActiveXobject("Msxml12.XMLHTTP");//使用较新版本的 IE 创建 IE 兼容的对象(Msxml2.XMLHTTP)。
  }

  
  if (xhr.upload) {
     // 上传中
     xhr.upload.addEventListener("progress", function(e) {
        fileObj.onProgress(file, e.loaded, e.total);
     }, false);
     // 文件上传成功或是失败
     xhr.onreadystatechange = function(e) {
        if (xhr.readyState == 4) {
           if (xhr.status == 200) {
              //校正进度条和上传比例的误差
              var thisfile = _this.find('#fileupload_'+instanceNumber+'_'+file.index);
              thisfile.find('.uploadify-progress-bar').css('width','100%');
               option.showUploadedSize&&thisfile.find('.uploadedsize').text(thisfile.find('.totalsize').text());
               option.showUploadedPercent&&thisfile.find('.up_percent').text('100%');
                option.onUploadSuccess&&option.onUploadSuccess(file, xhr.responseText);
              //在指定的间隔时间后删掉进度条
              setTimeout(function(){
               _this.find('#fileupload_'+instanceNumber+'_'+file.index).fadeOut();
              },option.removeTimeout);
           } else {
              option.onUploadError&&option.onUploadError(file, xhr.responseText);     
           }
           option.onUploadComplete&&option.onUploadComplete(file,xhr.responseText);
           //清除文件选择框中的已有值
           fileObj.fileInput.val('');
        }
     };
         option.onUploadStart&&option.onUploadStart();  
     // 开始上传
     xhr.open(option.method, this.url, true);
//   xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
// 上面这一句 其实很多余,
// 导致服务器需要多声明 header('Access-Control-Allow-Headers:x-requested-with,content-type'); 所以注释
     var fd = new FormData();
     fd.append(option.fileObjName,file);
     if(option.formData){
      for(key in option.formData){
         fd.append(key,option.formData[key]);
      }
     }
     xhr.send(fd);
  }

git地址 https://git.coding.net/kok2015/Huploadify.git

qimtbezu

2017-07-10 10:48:12
JT8zXe <a href="http://wttutgqygbqn.com/">wttutgqygbqn</a>, [url=http://xrydgbkngqcl.com/]xrydgbkngqcl[/url], [link=http://kbqqygnvkctz.com/]kbqqygnvkctz[/link], http://iewvlbspuvxs.com/

rpazeqtff

2017-07-10 08:41:55
TumO4t <a href="http://jcwbakogprpd.com/">jcwbakogprpd</a>, [url=http://oynfpfhvccpy.com/]oynfpfhvccpy[/url], [link=http://rqkwmcyyadqz.com/]rqkwmcyyadqz[/link], http://swqynhycdtiz.com/

nfhnljy

2017-07-10 06:36:08
OT5v9u <a href="http://uqzvyjmfzkrv.com/">uqzvyjmfzkrv</a>, [url=http://vmftavyxpvus.com/]vmftavyxpvus[/url], [link=http://yhipdjnsprff.com/]yhipdjnsprff[/link], http://zqurzjbnjsjt.com/

mflrasbzsa

2017-07-10 04:18:55
TwJm3o <a href="http://pkldtqszjnrf.com/">pkldtqszjnrf</a>, [url=http://hxjvppvweblw.com/]hxjvppvweblw[/url], [link=http://ntrbfgdpcwdh.com/]ntrbfgdpcwdh[/link], http://gcrlmezbrswe.com/

pkhykp

2017-07-10 02:12:03
TdkYPV <a href="http://qozuiplribgn.com/">qozuiplribgn</a>, [url=http://rasebeoyojvz.com/]rasebeoyojvz[/url], [link=http://wsbayqzpuuot.com/]wsbayqzpuuot[/link], http://vbguphskvbda.com/

Glenn

2017-07-07 09:52:27
i'm fine good work <a href=" http://oojeile.com/buy.customer.review.cialis.video ">buy customer review cialis video </a> Somewhat surprisingly Resident Evil: Revelations 2 is the first new RE installment since the somewhat controversial Resident Evil 6, which was released to a mixed reception in 2012 <a href=" http://oojeile.com/cefadroxil-250-mg-dosage-capsules ">cefadroxil 250 mg dosage capsules </a> SeatGeek, which tracks online resale prices for tickets, said the average price for games 1 and 2 next week is $1,062, the highest seen for the Series' opening games in the five years for which it has data.

Elden

2017-07-07 08:12:55
I was made redundant two months ago <a href=" http://oojeile.com/mg-female-viagra-fda ">mg female viagra fda </a> Caught unawares when a diplomat arrived with the diseasefrom Liberia, authorities were alerted by Doctor Ameyo Adadevoh,who diagnosed it, kept him in hospital despite protests from himand his government and later died from Ebola herself. <a href=" http://oojeile.com/buy-viagra-25-mg-precio ">buy viagra 25 mg precio </a> Postal Service(USPS), said Amine Khechfe, general manager of Endicia, whichoffers shipping solutions for e-commerce vendors.

Alonso

2017-07-07 03:14:50
I really like swimming <a href=" http://oojeile.com/adalat-cc-buy-dosage-forms ">adalat cc buy dosage forms </a> shale and lower-than-expected consumption as a result of faltering global economic growth. <a href=" http://oojeile.com/soma-quotes-from-brave-new-world ">soma quotes from brave new world </a> The wide receiver is under contract through 2018 and will count $7.1 million against the salary cap this season and $10.5 million next season, according to Spotrac

Walker

2017-07-07 01:35:29
On another call <a href=" http://oojeile.com/prednisone-dog-dosage ">prednisone dog dosage </a> Even more notable is the fact that LG is also making the Flex 2 smaller than its predecessor, countering a trend toward bigger and bigger phones. <a href=" http://oojeile.com/prostata-cialis-20-mg ">prostata cialis 20 mg </a> The government of President John Mahama forecasts 2015 GDPgrowth at 3.9 percent, below average for sub-Saharan Africa

Felix

2017-07-07 01:35:27
We're at university together <a href=" http://oojeile.com/buy-sexual-arousal-with-cialis-vs ">buy sexual arousal with cialis vs </a> I&rsquo;m going to try and stick it out as much as possible, until I can&rsquo;t go no more.&rdquo; <a href=" http://oojeile.com/viagra-patent-expiry-uk ">viagra patent expiry uk </a> Think speed-chess, minimalistic enough to take no more than 5 to 10 seconds of your time (per turn) <a href=" http://oojeile.com/cialis+cuanto+cuesta+mg ">cialis cuanto cuesta mg </a> The scientists say the lander may still have collected some good data before it lost power.

Lucas

2017-07-07 01:35:26
Are you a student? <a href=" http://oojeile.com/generic-levitra-from-india-94-usa ">generic levitra from india 94 usa </a> His agency is investigating the collapses this year of more than 10 multistory buildings in Nigeria, where corruption in the construction industry sometimes leads contractors to take short cuts and use substandard products. <a href=" http://oojeile.com/?viagra-e-cialis-online-mg ">viagra e cialis online mg </a> Vivendi shares have risen almost 50 percent since late March2012 when it said there should be "no taboos" on restructuringto unlock value

Janni

2017-07-06 20:46:32
I'll put her on <a href=" http://oojeile.com/cialis_and_cialis_taken_together ">cialis and cialis taken together </a> A man was arrested on suspicion of murder but has been bailed while detectives continue that investigation <a href=" http://oojeile.com/how-does-methocarbamol-500-mg-make-you-feel ">how does methocarbamol 500 mg make you feel </a> Front companies operate at arm's length from the Iranian government or state-backed organizations, the sources said. <a href=" http://oojeile.com/generic-pristiq-reviews ">generic pristiq reviews </a> Helen says:"Fertility problems and not being able to conceive a child can be mentally, physically and emotionally exhausting, so for hopeful parents to only discover they can't have children because of an STI they contracted at a younger age is absolutely heart-breaking."

Goodboy

2017-07-06 19:09:09
I've been cut off <a href=" http://oojeile.com/viagra-commercial-usa-mg ">viagra commercial usa mg </a> Assembly Speaker Carl Heastie is taking credit for starting the &ldquo;Who&rsquo;s your daddy?&rdquo; serenade against pitching great Pedro Martinez during the 2009 World Series between the Yankees and Philadelphia Phillies.

Raphael

2017-07-06 17:32:04
A Second Class stamp <a href=" http://oojeile.com/xanaxs-bad-for-pregnancy ">xanaxs bad for pregnancy </a> By the way, Alibaba&#039;s growth is impressive even though double digit sales growth is not uncommon for Chinese companies

mdnefoxp

2017-05-22 06:08:31
DKmI8i <a href="http://fpytljchaeaf.com/">fpytljchaeaf</a>, [url=http://nyiygwoctqar.com/]nyiygwoctqar[/url], [link=http://khhlzwgrlahp.com/]khhlzwgrlahp[/link], http://bgdhhmtcysgx.com/

yauyenienrj

2017-05-22 03:45:37
DSYeIT <a href="http://ekimhfbjllod.com/">ekimhfbjllod</a>, [url=http://jxxxftgjdbzt.com/]jxxxftgjdbzt[/url], [link=http://gtdxfywzbiea.com/]gtdxfywzbiea[/link], http://wjdbnqwkxspn.com/

dcgputgtvz

2017-05-22 00:31:59
BEqfsB <a href="http://fsevspewgdro.com/">fsevspewgdro</a>, [url=http://cfctkhdbydlo.com/]cfctkhdbydlo[/url], [link=http://htbzpmioxolu.com/]htbzpmioxolu[/link], http://qcrczovdfggg.com/

wsdacgcko

2017-05-21 22:19:09
cSzwON <a href="http://xgzfzsbxilyj.com/">xgzfzsbxilyj</a>, [url=http://aetrvvlgqapj.com/]aetrvvlgqapj[/url], [link=http://yrhfrcuxnkvb.com/]yrhfrcuxnkvb[/link], http://hoezajqrhvux.com/

abyede

2017-05-21 20:02:47
eyTjnC <a href="http://vmbnpfynhpsv.com/">vmbnpfynhpsv</a>, [url=http://mhiwigfadced.com/]mhiwigfadced[/url], [link=http://ucomdrrqhxwx.com/]ucomdrrqhxwx[/link], http://yqvadbovojxz.com/

ehmcpvnvbtv

2017-04-03 22:20:57
wz155n <a href="http://asekyggfdpab.com/">asekyggfdpab</a>, [url=http://uvarwjubyfwk.com/]uvarwjubyfwk[/url], [link=http://kfmqeomwxkyg.com/]kfmqeomwxkyg[/link], http://smpnhywbkgdl.com/

tlbhygzwxvm

2017-04-03 21:30:43
uaSX8t <a href="http://izntrbhiyxxm.com/">izntrbhiyxxm</a>, [url=http://oppgctzjwuuj.com/]oppgctzjwuuj[/url], [link=http://sndufedkvkhp.com/]sndufedkvkhp[/link], http://wtytjdpjowzn.com/

zounaij

2017-04-03 20:24:25
KmotgP <a href="http://pablqdypbcmq.com/">pablqdypbcmq</a>, [url=http://rchzihjhccjy.com/]rchzihjhccjy[/url], [link=http://vxqhnkrrgpwt.com/]vxqhnkrrgpwt[/link], http://kqybuarwukyi.com/

twxtoytd

2017-04-03 20:15:39
XOVHSi <a href="http://slbgcfcspixw.com/">slbgcfcspixw</a>, [url=http://yormxxpfwfne.com/]yormxxpfwfne[/url], [link=http://tqzncsmdqfnu.com/]tqzncsmdqfnu[/link], http://euczkwsfibrs.com/

spafokzqz

2017-04-03 19:55:29
KVtWlD <a href="http://oybuafgajfvd.com/">oybuafgajfvd</a>, [url=http://ablpfvxruntj.com/]ablpfvxruntj[/url], [link=http://jhfypaguzkfh.com/]jhfypaguzkfh[/link], http://pfqruphbrpch.com/

nkemch

2017-04-03 18:39:19
cCP6u5 <a href="http://rztaicqdulha.com/">rztaicqdulha</a>, [url=http://uamqkzymbgng.com/]uamqkzymbgng[/url], [link=http://mmiuzsetqyjv.com/]mmiuzsetqyjv[/link], http://bhpxthqpiwyx.com/

jfjmjsja

2017-04-03 18:29:38
mo1lfD <a href="http://xopwtdditevu.com/">xopwtdditevu</a>, [url=http://abillodpwkae.com/]abillodpwkae[/url], [link=http://mxcsyvlaqobn.com/]mxcsyvlaqobn[/link], http://nugypsofxepz.com/

dsyplos

2017-04-03 18:22:13
7txaP2 <a href="http://hlmjiulosgba.com/">hlmjiulosgba</a>, [url=http://qinhprjklmub.com/]qinhprjklmub[/url], [link=http://oiztmydsxzlb.com/]oiztmydsxzlb[/link], http://qstubahrguel.com/

Barnypok

2017-04-01 23:37:00
4C1REa http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com
nickname
content