Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery.post的Firefox问题_Javascript_Jquery_Post - Fatal编程技术网

Javascript jquery.post的Firefox问题

Javascript jquery.post的Firefox问题,javascript,jquery,post,Javascript,Jquery,Post,我有一个函数,可以从控制器中检索base64编码的图像,并在新窗口中显示该图像。它在Chrome中工作得很好,但在Firefox中失败,出现错误“格式不正确”和“请求方法”GET“不受支持”。我不明白为什么会这样,因为我使用的是post,而不是get 相关代码部分为: function showPicture(label) { $.post( 'picture-view', { labelname: label }, function(ImageSrc) { var img = new I

我有一个函数,可以从控制器中检索base64编码的图像,并在新窗口中显示该图像。它在Chrome中工作得很好,但在Firefox中失败,出现错误“格式不正确”和“请求方法”GET“不受支持”。我不明白为什么会这样,因为我使用的是post,而不是get

相关代码部分为:

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应该是字符串,但被解释为XML对象。它包含反斜杠的事实导致了“格式不正确”错误。我现在的问题是:如何让$.post或$.ajax在Firefox中返回字符串(他们在Chrome中已经这样做了)。

我可能会尝试使用
.ajax
。它还允许您提供备用内容

与其使用post发送图像,不如在服务器上托管图像并通过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);

    }

});

为了不留下未回答的问题


我最终使用了一个lightbox来显示图像,而不是生成一个新窗口

谢谢你的建议Sam。我试过使用.ajax,但奇怪的是错误函数没有被调用。所以它会出现(当我将img.src写入控制台时就证明了)。它一定是成功函数中的某个东西。那么,给我一点时间测试一下,我会更新我的答案。恐怕不走运,也许你可以从中找到一些东西?->谢谢你的尝试,非常感谢。如果这让问题变得更清楚,那么问题似乎在于数据变量没有正确填充。当使用Chrome时,我得到的是base64编码的图像,但当使用Firefox时,返回的是[objectXMLDocument]。