久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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="qwawimqqmiuu" 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="qwawimqqmiuu" 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 代碼發送到 django 視圖.我需要檢測該文件是否已被發送或如果發生錯誤.我使用 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 視圖 發回序列化的 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"}
                  

                  我想知道的是,這是否是正確的方法.在許多文章中,我發現警告

                  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()異步的同時查出文件是否已經發送?

                  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 視圖從表單數據中提取文件并寫入目標文件夾.

                  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模板網!

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

                  相關文檔推薦

                  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屋-程序員軟件開發技術分
                  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="qwawimqqmiuu" 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>
                            主站蜘蛛池模板: 欧美a在线| 国产欧美日韩一区 | 国产一区二区三区视频 | 伊人网伊人网 | 国产91在线 | 亚洲 | 伊人看片 | 亚洲国产成人久久综合一区,久久久国产99 | 中文字幕一区二区三区四区五区 | 亚洲精品一区在线 | 男女视频在线观看网站 | 99国产精品久久久久老师 | 免费午夜电影 | 精品美女视频在线观看免费软件 | 亚洲永久 | 一级一级一级毛片 | 在线一区视频 | 美女视频黄色片 | 特黄特色大片免费视频观看 | 亚洲精品国产成人 | 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 91日韩在线 | 一区二区免费视频 | 精品在线一区二区三区 | 国产精品毛片一区二区三区 | 欧美在线 | 中文字幕乱码一区二区三区 | 国产欧美一区二区三区久久 | 天堂综合网 | 黄色成人国产 | 国产精品成人一区二区三区 | 日韩精品视频在线播放 | 九九在线精品视频 | 亚洲精品 在线播放 | 在线播放一区二区三区 | 欧美午夜剧场 | 日本成人中文字幕在线观看 | xxxxx免费视频 | 毛片在线看片 | 福利久久 | 本道综合精品 | 成人福利网站 |