Javascript 如何使用 AJAX 打开 PDF 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13988249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:21:30  来源:igfitidea点击:

How to open the PDF file using AJAX

javascriptajaxjquery

提问by SahanS

How to open the PDF file using AJAX I tried this

如何使用 AJAX 打开 PDF 文件我试过了

$.ajax({

          type : 'GET',
          url : ApplicationParameters.getWebRoot() + 'E_Books/pdf/previews/' + jsonRecord[0].previewUrl,
          data : strJsonParam,
          dataType : 'text',
          contentType : 'application/pdf',

          success: function(data){
          var opn = open("/E_Books/pdf/previews/"+jsonRecord[0].previewUrl);
          displayBook(opn);
          ebookStore.add(opn);
          ebookStore.sync();
    }
    ...

it open pdf file but I want open that in same window.it open in another window. help please

它打开 pdf 文件,但我想在同一个窗口中打开它。它在另一个窗口中打开。请帮忙

回答by

Replace your code with this:

用这个替换你的代码:

            type : 'GET',
            url : ApplicationParameters.getWebRoot() + 'E_Books/pdf/previews/' + jsonRecord[0].previewUrl,
            data : strJsonParam,
            dataType : 'text',
            contentType : 'application/pdf',

            success: function(data){
            var opn = open("/E_Books/pdf/previews/"+jsonRecord[0].previewUrl, "_self");
                    displayBook(opn);
                    ebookStore.add(opn);
                    ebookStore.sync();
}

回答by mortb

You don't need ajax for this. In your code above it is the line var opn = open("/E_Books/pdf/previews/"+jsonRecord[0].previewUrl);that opens your pdf in a new window. Not the ajax call.

为此,您不需要 ajax。在上面的代码中,它是opn = open("/E_Books/pdf/previews/"+jsonRecord[0].previewUrl);在新窗口中打开 pdf的行 var 。不是 ajax 调用。

Just set document.location = "/E_Books/pdf/previews/"+jsonRecord[0].previewUrlto replace the page that is displayed in the browser with your pdf.

只需设置document.location = "/E_Books/pdf/previews/"+jsonRecord[0].previewUrl为用您的 pdf 替换浏览器中显示的页面即可。

回答by Karthika Subramanian

You can try using this..

你可以试试用这个..

window.location.href="/E_Books/pdf/previews/"+jsonRecord[0].previewUrl";

or

或者

window.location="/E_Books/pdf/previews/"+jsonRecord[0].previewUrl";