pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <legend id='ZThNt'><style id='ZThNt'><dir id='ZThNt'><q id='ZThNt'></q></dir></style></legend>
    <tfoot id='ZThNt'></tfoot>

    • <bdo id='ZThNt'></bdo><ul id='ZThNt'></ul>
  • <small id='ZThNt'></small><noframes id='ZThNt'>

        <i id='ZThNt'><tr id='ZThNt'><dt id='ZThNt'><q id='ZThNt'><span id='ZThNt'><b id='ZThNt'><form id='ZThNt'><ins id='ZThNt'></ins><ul id='ZThNt'></ul><sub id='ZThNt'></sub></form><legend id='ZThNt'></legend><bdo id='ZThNt'><pre id='ZThNt'><center id='ZThNt'></center></pre></bdo></b><th id='ZThNt'></th></span></q></dt></tr></i><div class="ckkac4i" id='ZThNt'><tfoot id='ZThNt'></tfoot><dl id='ZThNt'><fieldset id='ZThNt'></fieldset></dl></div>

        如何確定 XMLHttpRequest.send() 是否有效

        how to find out if XMLHttpRequest.send() worked(如何確定 XMLHttpRequest.send() 是否有效)

              <tfoot id='aVzgm'></tfoot>
              <legend id='aVzgm'><style id='aVzgm'><dir id='aVzgm'><q id='aVzgm'></q></dir></style></legend>
              <i id='aVzgm'><tr id='aVzgm'><dt id='aVzgm'><q id='aVzgm'><span id='aVzgm'><b id='aVzgm'><form id='aVzgm'><ins id='aVzgm'></ins><ul id='aVzgm'></ul><sub id='aVzgm'></sub></form><legend id='aVzgm'></legend><bdo id='aVzgm'><pre id='aVzgm'><center id='aVzgm'></center></pre></bdo></b><th id='aVzgm'></th></span></q></dt></tr></i><div class="u2mmgk2" id='aVzgm'><tfoot id='aVzgm'></tfoot><dl id='aVzgm'><fieldset id='aVzgm'></fieldset></dl></div>

              • <bdo id='aVzgm'></bdo><ul id='aVzgm'></ul>
                    <tbody id='aVzgm'></tbody>
                1. <small id='aVzgm'></small><noframes id='aVzgm'>

                  本文介紹了如何確定 XMLHttpRequest.send() 是否有效的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在使用 XMLHttpRequest 將文件從 javascript 代碼發(fā)送到 django 視圖.我需要檢測該文件是否已被發(fā)送或如果發(fā)生錯誤.我使用 jquery 編寫以下 javascript.

                  I am using XMLHttpRequest to send a file from javascript code to a django view.I need to detect,whether the file has been sent or if some error occurred.I used jquery to write the following javascript.

                  理想情況下,我想向用戶顯示文件未上傳的錯誤消息.有沒有辦法在 javascript 中做到這一點?

                  Ideally I would like to show the user an error message that the file was not uploaded.Is there some way to do this in javascript?

                  我試圖通過從 django view 返回 success/failure 消息來做到這一點,將 success/failed 消息 作為 json 并從 django 視圖 發(fā)回序列化的 json.為此,我制作了 xhr.open() non-asynchronous.我試圖打印 xmlhttpRequest 對象的 responseText . console.log(xhr.responseText) 顯示

                  I tried to do this by returning a success/failure message from django view , putting the success/failed message as json and sending back the serialized json from the django view.For this,I made the xhr.open() non-asynchronous. I tried to print the xmlhttpRequest object's responseText .The console.log(xhr.responseText) shows

                  response= {"message": "success"}
                  

                  我想知道的是,這是否是正確的方法.在許多文章中,我發(fā)現(xiàn)警告

                  What I am wondering is,whether this is the proper way to do this.In many articles,I found the warning that

                  不推薦使用 async=false

                  Using async=false is not recommended

                  那么,有沒有什么辦法可以在保持xhr.open()異步的同時查出文件是否已經(jīng)發(fā)送?

                  So,is there any way to find out whether the file has been sent,while keeping xhr.open() asynchronous?

                  $(document).ready(function(){
                     $(document).on('change', '#fselect', function(e){
                              e.preventDefault();
                              sendFile();
                          });
                  });
                  
                  function sendFile(){
                     var form = $('#fileform').get(0);
                     var formData = new FormData(form);
                     var file = $('#fselect').get(0).files[0];
                     var xhr = new XMLHttpRequest();
                     formData.append('myfile', file);
                     xhr.open('POST', 'uploadfile/', false);
                     xhr.send(formData);
                     console.log('response=',xhr.responseText);
                  }
                  

                  我的 django 視圖從表單數(shù)據(jù)中提取文件并寫入目標文件夾.

                  My django view extracts file from form data and writes to a destination folder.

                  def store_uploaded_file(request):
                     message='failed'
                     to_return = {}
                     if  (request.method == 'POST'):          
                        if request.FILES.has_key('myfile'):
                           file = request.FILES['myfile']
                           with open('/uploadpath/%s' % file.name, 'wb+') as dest:
                              for chunk in file.chunks():
                                 dest.write(chunk)
                                 message="success"
                     to_return['message']= message
                     serialized = simplejson.dumps(to_return)
                     if store_message == "success":
                        return HttpResponse(serialized, mimetype="application/json")
                     else:
                        return HttpResponseServerError(serialized, mimetype="application/json")
                  

                  我在 @FabrícioMatté 的幫助下完成了這項工作

                  I got this working with the help of @FabrícioMatté

                  xhr.onreadystatechange=function(){
                         if (xhr.readyState==4 && xhr.status==200){
                            console.log('xhr.readyState=',xhr.readyState);
                            console.log('xhr.status=',xhr.status);
                            console.log('response=',xhr.responseText);
                  
                            var data = $.parseJSON(xhr.responseText);
                            var uploadResult = data['message']
                            console.log('uploadResult=',uploadResult);
                  
                            if (uploadResult=='failure'){
                               console.log('failed to upload file');
                               displayError('failed to upload');
                            }else if (uploadResult=='success'){
                               console.log('successfully uploaded file');
                            }
                         }
                      }
                  

                  推薦答案

                  XMLHttpRequest 對象包含 statusreadyState 屬性,您可以在 xhr.onreadystatechange 事件中進行測試以檢查您的請求是否成功.

                  XMLHttpRequest objects contain the status and readyState properties, which you can test in the xhr.onreadystatechange event to check if your request was successful.

                  這篇關于如何確定 XMLHttpRequest.send() 是否有效的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發(fā)技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                2. <legend id='WysOo'><style id='WysOo'><dir id='WysOo'><q id='WysOo'></q></dir></style></legend>
                    <tbody id='WysOo'></tbody>

                    <small id='WysOo'></small><noframes id='WysOo'>

                    • <i id='WysOo'><tr id='WysOo'><dt id='WysOo'><q id='WysOo'><span id='WysOo'><b id='WysOo'><form id='WysOo'><ins id='WysOo'></ins><ul id='WysOo'></ul><sub id='WysOo'></sub></form><legend id='WysOo'></legend><bdo id='WysOo'><pre id='WysOo'><center id='WysOo'></center></pre></bdo></b><th id='WysOo'></th></span></q></dt></tr></i><div class="kki20ey" id='WysOo'><tfoot id='WysOo'></tfoot><dl id='WysOo'><fieldset id='WysOo'></fieldset></dl></div>

                      <tfoot id='WysOo'></tfoot>

                            <bdo id='WysOo'></bdo><ul id='WysOo'></ul>
                            主站蜘蛛池模板: 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 济南轻型钢结构/济南铁艺护栏/济南铁艺大门-济南燕翔铁艺制品有限公司 | 自清洗过滤器_全自动过滤器_全自动反冲洗过滤器_量子过滤器-滑漮滴 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 山西3A认证|太原AAA信用认证|投标AAA信用证书-山西AAA企业信用评级网 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 钢结构厂房造价_钢结构厂房预算_轻钢结构厂房_山东三维钢结构公司 | 加热制冷恒温循环器-加热制冷循环油浴-杭州庚雨仪器有限公司 | 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 细胞染色-流式双标-试剂盒免费代做-上海研谨生物科技有限公司 | 东莞办公家具厂家直销-美鑫【免费3D效果图】全国办公桌/会议桌定制 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 存包柜厂家_电子存包柜_超市存包柜_超市电子存包柜_自动存包柜-洛阳中星 | 氧化锆陶瓷_氧化锆陶瓷加工_氧化锆陶瓷生产厂家-康柏工业陶瓷有限公司 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 烟气换热器_GGH烟气换热器_空气预热器_高温气气换热器-青岛康景辉 | 基业箱_环网柜_配电柜厂家_开关柜厂家_开关断路器-东莞基业电气设备有限公司 | 大数据营销公司_舆情监测软件_上海SEO公司-文军营销官网 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 万濠投影仪_瑞士TRIMOS高度仪_尼康投影仪V12BDC|量子仪器 | 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 四川成都干燥设备_回转筒干燥机_脉冲除尘器_输送设备_热风炉_成都川工星科机电设备有限公司 | hdpe土工膜-防渗膜-复合土工膜-长丝土工布价格-厂家直销「恒阳新材料」-山东恒阳新材料有限公司 ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 昆明化妆培训-纹绣美甲-美容美牙培训-昆明博澜培训学校 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 寮步纸箱厂_东莞纸箱厂 _东莞纸箱加工厂-东莞市寮步恒辉纸制品厂 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 桂林腻子粉_内墙外墙抗裂砂浆腻子粉推荐广西鑫达涂料厂家供应 | 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | J.S.Bach 圣巴赫_高端背景音乐系统_官网 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 苏州柯瑞德货架-仓库自动化改造解决方案 | 步进电机_agv电机_伺服马达-伺服轮毂电机-和利时电机 |