简体   繁体   English

load pdf in javascript错误,但是为什么直接在浏览器中就成功了

[英]load pdf in javascript error, but why directly in browser successfully

all.全部。 I am thinking about a issue using pdfTron SDK(an SDK help load pdf) in my application.我正在考虑在我的应用程序中使用 pdfTron SDK(SDK 帮助加载 pdf)的问题。 This SDK is able to load local pdf files, working like a charm.这个 SDK 能够加载本地 pdf 文件,工作起来很有魅力。 I decide to load an online pdf file in my web application, using pdfTron SDK, eg 'https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf'.我决定在我的 web 应用程序中加载在线 pdf 文件,使用 pdfTron SDK,例如“https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf”。 This file is accessible in browser, directly, but when I use scripts, like:这个文件可以在浏览器中直接访问,但是当我使用脚本时,比如:

instance.UI.loadDocument('https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf');
  };

I directly got an error when running the application, which shows我在运行应用程序的时候直接报错,显示

Request URL: https://cdn.mendix.tencent-cloud.com/documentation/developerportal/tencent-deploy.pdf
Request Method: HEAD
Status Code: 403 
Referrer Policy: strict-origin-when-cross-origin

I think this is a CORS issue.我认为这是一个 CORS 问题。 But I cannot understand why I can use browser getting this file, while scripts does not.但我不明白为什么我可以使用浏览器获取此文件,而脚本却不能。 How can I resolve this?我该如何解决这个问题? Thanks谢谢

I would suggest to read the documentation on pdfTron concerning "webview" and "loadDocment".我建议阅读 pdfTron 上有关“webview”和“loadDocment”的文档。

WebView is used to get the pdf you want to use from any source(file or web), in your case, you need to use "initialDoc" property, that is where you would place "cdn.mendix.tencent-cloud.com/documentation/developerportal/…". WebView 用于从任何来源(文件或网络)获取您要使用的 pdf,在您的情况下,您需要使用“initialDoc”属性,即放置“cdn.mendix.tencent-cloud.com/文档/开发者门户网站/…”。

Then "instance.UI.loadDocument" is used to identify the html element where you want to place the fetched document.然后“instance.UI.loadDocument”用于标识要放置获取的文档的 html 元素。

Sample 样本

API Docs API 文档

I also try a CORS config in my document server(nginx), like this:我还在我的文档服务器 (nginx) 中尝试 CORS 配置,如下所示:

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

This works now.这现在有效。 But It seems that if the document server is not yours, you cannot do this.但是好像如果文档服务器不是你的,你就不能这样做。 Hope this help!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击链接时出现403禁止错误。直接在浏览器中输入网址后,效果很好 - 403 Forbidden Error when I click on the link.Works Fine when enter the url in browser directly 为什么我可以从浏览器访问 url 但从节点(axios.get)得到 403 错误(API) - Why I can acces to url from browser but from node(axios.get) getting 403 error (API) Java禁止代码错误,但浏览器错误(2) - Java forbidden error with code but not with browser (2) 使用Chrome pdf查看器打开PDF时出现403错误 - 403 error when opening PDF with chrome pdf viewer 403 Forbidden错误的浏览器行为 - Browser behavior on 403 Forbidden error HTTP错误403:禁止,BlackBerry Browser - HTTP Error 403: Forbidden , BlackBerry Browser Java应用程序中出现403错误,但浏览器或邮递员中没有403错误 - 403 error in Java Application but not in browser or Postman JavaScript图像预加载和403错误 - JavaScript image preloading and 403 error 403 Forbidden:可以在浏览器中打开uri,但不能在JavaScript代码中打开 - 403 Forbidden: Able to open uri in browser but not in JavaScript code 浏览器中的 http 状态码为 200,而 http 状态检查器中的状态码为 403,为什么? - http status code 200 in browser, but 403 in http status checker, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM