2012-12-21 32 views
2

我正在使用YUI3 IO實用程序來提交帶有文件的表單。來自服務器的響應包含HTML,但在訪問請求對象時會被剝離。YUI 3 IO實用程序 - 使用文件上傳的表單提交從responseText中刪除HTML

var form = Y.one('form'); 
Y.io(form.get('action'), { 
    method: form.get('method'), 
    form: { 
     id: form, 
     upload: true 
    }, 
    on: { 
     complete: function(id, request) { 
      // The server returns a response like <div>response</div> 
      console.log(request.responseText); 
      // All HTML is stripped so it just prints 'response' to the console 
     } 
    } 
}); 

這是否被認爲是默認行爲?我幾乎不能相信它是不可能獲得完整的響應內容... 服務器還將內容類型標頭正確設置爲「text/html」。

任何幫助表示讚賞!謝謝!

回答

1

我只看了YUI3的源代碼。在幾行負責請求對象中https://raw.github.com/yui/yui3/master/src/io/js/io-upload-iframe.js是:

_uploadComplete: function(o, c) { 
    var io = this, 
     d = Y.one('#io_iframe' + o.id).get('contentWindow.document'), 
     b = d.one('body'), 
     p; 

    if (c.timeout) { 
     io._clearUploadTimeout(o.id); 
    } 

    try { 
     if (b) { 
      // When a response Content-Type of "text/plain" is used, Firefox and  Safari 
      // will wrap the response string with <pre></pre>. 
      p = b.one('pre:first-child'); 
      o.c.responseText = p ? p.get('text') : b.get('text'); 
      Y.log('The responseText value for transaction ' + o.id + ' is: ' +  o.c.responseText + '.', 'info', 'io'); 
     } 
     else { 
      o.c.responseXML = d._node; 
      Y.log('The response for transaction ' + o.id + ' is an XML  document.', 'info', 'io'); 
     } 
    } 
    catch (e) { 
     o.e = "upload failure"; 
    } 

    io.complete(o, c); 
    io.end(o, c); 
    // The transaction is complete, so call _dFrame to remove 
    // the event listener bound to the iframe transport, and then 
    // destroy the iframe. 
    w.setTimeout(function() { _dFrame(o.id); }, 0); 
}, 

因此,一旦響應包含它返回主體內容爲「文本」「身體」節點。

o.c.responseText = p ? p.get('text') : b.get('text'); 

恕我直言,沒有機會得到innerHTML,如果有一個身體節點。我決定創建一個定製的IO Upload Iframe模塊,該模塊添加一個名爲「responseHTML」的附加屬性與body節點的innerHTML。

您可以從引擎收錄獲得源:http://pastebin.com/WadQgNP2

0

http://yuilibrary.com/yui/docs/io/#the-response-object

responseXML屬性可能包含你正在尋找的東西。如果這無助於嘗試更改console.log(request.responseText);console.log(request);並在調試器(例如Firebug)中查看它的屬性以找到包含所需數據的屬性。

請記住,一般來說,所有名爲...Text的屬性都只會返回某些東西的文本內容,並且完全沒有XML代碼。

+0

謝謝您的回答。不幸的是,它甚至沒有包含reponseXML屬性。我最終創建了一個修改後的IO Upload Iframe模塊,該模塊返回innerHTML作爲附加屬性(responseHTML)。我會將其添加爲我的問題的答案。 – Pascal