2012-07-17 60 views
1

我有一個功能,它從控制器中檢索base64編碼圖像,並在新窗口中顯示圖像。它在Chrome中運行良好,但在Firefox中出現錯誤「格式不正確」和「請求方法」GET不支持「。我不明白爲什麼會的情況作了說明,我使用後,沒有得到......Firefox問題與jquery.post

相關的代碼段是:

function showPicture(label) { 
$.post('picture-view', { labelname: label }, function(ImageSrc) { 
    var img = new Image; 
     img.src = "data:image/png;base64," + ImageSrc; 
     img.style.position = 'fixed'; 
     img.style.left = '0'; 
     img.style.top = '0'; 
     img.onload = function() {   
     var newWindow = window.open("", label,"scrollbars=0, toolbar=0, width="+myImage.width+", height="+myImage.height); 
          creativeWindow.document.documentElement.innerHTML=''; 
          creativeWindow.document.documentElement.appendChild(img); 
     }​ 
});} 

編輯另外,對於訪問書籍的任何建議或網站了解更多關於這種材料將受到感謝。

編輯#2我還應該補充一點,當我嘗試將img.src寫入控制檯時,它以[objectXMLDocument]出現。

編輯#3我已經跟蹤了錯誤,因爲返回的ImageSrc應該是一個String,但被解釋爲一個XML對象。它包含反斜槓的事實導致了「不完整」的錯誤。我現在的問題是:如何獲得$ .post或$ .ajax在Firefox中返回一個字符串(他們已經在Chrome中這樣做了)。

回答

0

在不會離開一個懸而未決的問題的興趣...

我結束了使用燈箱來顯示圖像,而不是產生一個新的窗口。

0

我可能會嘗試使用.ajax來代替。它可以讓你提供回退內容。

而不是發送圖像使用後,爲什麼不託管您的服務器上的圖像,並通過src加載圖像。這將是一個更清潔的方法。

例如

$.ajax({ 

    type: "POST", 
    data: "", 
    url: yourURL, 
    error: function() { 
     //The call was unsuccessful 
     //Place fallback content/message here 
    }, 
    success: function(data) { 
     //The call was successful 

     //Open new window 
     var newWindow = window.open("", label,"scrollbars=0, toolbar=0, width="+myImage.width+", height="+myImage.height); 
     creativeWindow.document.documentElement.innerHTML=''; 

     //Create the image 
     var image = document.createElement("img"); 
     image.src = data; 

     //Attach image to new window 
     creativeWindow.document.documentElement.appendChild(image); 

    } 

}); 
+0

感謝您的建議山姆。我嘗試使用.ajax,但奇怪的是沒有調用錯誤函數。 – elo96 2012-07-17 12:59:45

+0

被稱爲成功嗎? – Undefined 2012-07-17 13:02:39

+0

所以它會出現(證明當我寫img.src到控制檯)。 – elo96 2012-07-17 13:03:14