我没有在控制台中发现任何错误,但我想要的只是thumbnail_url,它是php文件中的json。在控制台中显示它是一个数组字符串,我该如何访问它?使用ajax从API获取图像
EDITS: 我能够执行我想要的输出,但问题是最后一个对象是唯一一个在浏览器中输出。
<?php
//Set API user and password
$API_USER = 'username';
$API_PASS = 'password';
//Set parameters to make cURL call to Duda
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'sampleurl');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $API_USER.':'.$API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//execute cURL call and get template data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
//echo $output;
print_r($output);
?>
这里是我们的HTML和Ajax请求:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Ajax Preview</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
$(function() {
var $data = $('#content');
$.ajax({
method:'GET',
dataType:'json',
url:'https://syncslider-ehnoxx07.c9users.io/temp.json',
success: function (data) {
$.each(data, function(i,data){
$data.html('<img src=' + data.thumbnail_url + '>');
});
}
});
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
你也可以在这里显示数组输出吗? – vher2
你已经发布了你的API和API凭证..我不会在公开帖子中推荐它。 –
这些Api凭据是虚拟的,但感谢您对其进行编辑。 下面是php截图: https://postimg.org/image/4svg44ubn/ 和网络中的html输出。 https://postimg.org/image/b5vobivxr/ – camdev