无法使用 jQuery 访问 API
Can't Access to API using jQuery
我正在使用 pCloud Api 从请求中下载 link。这是一个 GET 请求。当我请求表单浏览器时,我可以获得响应。但是当我使用 jQuery 时,我得到一个响应代码 result : 7010
Api 请求 URL : https://api.pcloud.com/getpublinkdownload?code=8eM7
我从浏览器请求时收到此响应:
{
"result": 0,
"expires": "Mon, 07 Aug 2017 00:12:50 +0000",
"dwltag": "aftsTab2SLkC4MDXRdp6Am",
"path": "\/cBZkvG2cXZNPjykVZZZChTDE7ZNVZZj5JZkZSqfRZIXZqkZmVZR7Zd7Z4ZfkZIZyVZokZbXZ3VZFkZ77ZIgCcZ14l5zXbx6p4GwdeEPdF1707nIPm7\/image%20%286%29.jpg",
"hosts": [
"p-def2.pcloud.com",
"c166.pcloud.com"
]
}
我需要这个 hosts
和 path
来生成下载 link。我只需要这个 -https://c166.pcloud.com/cBZkvG2cXZNPjykVZZZChTDE7ZNVZZj5JZkZSqfRZIXZqkZmVZR7Zd7Z4ZfkZIZyVZokZbXZ3VZFkZ77ZIgCcZ14l5zXbx6p4GwdeEPdF1707nIPm7/image%20%286%29.jpg
我必须使用 jQuery/JavaScript 才能获得此响应。我试过 PHP file_get_contents();
它可以工作,但是这个 link 只能从你请求的 ip 地址中工作。所以,我必须使用 JQ/JS.
我的代码:
$(document).ready(function(){
function httpGet(theUrl){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
console.log(httpGet("https://api.pcloud.com/getpublinkdownload?code=8eM7"));
});
感谢您的帮助。
pCloud 服务器似乎正在检查引荐来源网址。
大多数情况下,服务器会拒绝非自身的访问。
只有来自一小部分已批准(登录)页面的 Web 浏览器才能访问;这有助于在来自 https://en.wikipedia.org/wiki/HTTP_referer
的一组合作付费站点之间共享材料
在以下 html 中,脚本 运行 并成功获取图像 url,但浏览器在尝试加载图像时出现错误。
<html>
<head>
</head>
<script
src="http://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<body>
<h1>Load Image from pCloud </h1>
<img class="loading">
<script>
$(document).ready(function() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText){
var host = JSON.parse(this.responseText).hosts[0];
var path = JSON.parse(this.responseText).path;
}
$(".loading").attr("src", "https://" + host + path);
}
};
xhttp.open("GET", "https://api.pcloud.com/getpublinkdownload?code=8eM7", true);
xhttp.send();
});
</script>
</body>
</html>
我正在使用 pCloud Api 从请求中下载 link。这是一个 GET 请求。当我请求表单浏览器时,我可以获得响应。但是当我使用 jQuery 时,我得到一个响应代码 result : 7010
Api 请求 URL : https://api.pcloud.com/getpublinkdownload?code=8eM7
我从浏览器请求时收到此响应:
{
"result": 0,
"expires": "Mon, 07 Aug 2017 00:12:50 +0000",
"dwltag": "aftsTab2SLkC4MDXRdp6Am",
"path": "\/cBZkvG2cXZNPjykVZZZChTDE7ZNVZZj5JZkZSqfRZIXZqkZmVZR7Zd7Z4ZfkZIZyVZokZbXZ3VZFkZ77ZIgCcZ14l5zXbx6p4GwdeEPdF1707nIPm7\/image%20%286%29.jpg",
"hosts": [
"p-def2.pcloud.com",
"c166.pcloud.com"
]
}
我需要这个 hosts
和 path
来生成下载 link。我只需要这个 -https://c166.pcloud.com/cBZkvG2cXZNPjykVZZZChTDE7ZNVZZj5JZkZSqfRZIXZqkZmVZR7Zd7Z4ZfkZIZyVZokZbXZ3VZFkZ77ZIgCcZ14l5zXbx6p4GwdeEPdF1707nIPm7/image%20%286%29.jpg
我必须使用 jQuery/JavaScript 才能获得此响应。我试过 PHP file_get_contents();
它可以工作,但是这个 link 只能从你请求的 ip 地址中工作。所以,我必须使用 JQ/JS.
我的代码:
$(document).ready(function(){
function httpGet(theUrl){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
console.log(httpGet("https://api.pcloud.com/getpublinkdownload?code=8eM7"));
});
感谢您的帮助。
pCloud 服务器似乎正在检查引荐来源网址。 大多数情况下,服务器会拒绝非自身的访问。
只有来自一小部分已批准(登录)页面的 Web 浏览器才能访问;这有助于在来自 https://en.wikipedia.org/wiki/HTTP_referer
的一组合作付费站点之间共享材料在以下 html 中,脚本 运行 并成功获取图像 url,但浏览器在尝试加载图像时出现错误。
<html>
<head>
</head>
<script
src="http://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<body>
<h1>Load Image from pCloud </h1>
<img class="loading">
<script>
$(document).ready(function() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText){
var host = JSON.parse(this.responseText).hosts[0];
var path = JSON.parse(this.responseText).path;
}
$(".loading").attr("src", "https://" + host + path);
}
};
xhttp.open("GET", "https://api.pcloud.com/getpublinkdownload?code=8eM7", true);
xhttp.send();
});
</script>
</body>
</html>