JSON调用url来获取数据

时间:2017-01-20 10:54:28

标签: php json ajax xmlhttprequest jsonp

我从网站获取数据时遇到问题,我希望将其数据发送到我的网站 http://www.cvedetails.com/vulnerability-feeds-form.php

这是代码 `

<!DOCTYPE html>
<html>
<head>
    <title>json data</title>
</head>
<body>
<div class="demo" id="demo">
    <!-- <iframe src="http://www.cvedetails.com/widget.php?numrows=10&vendor_id=0&product_id=0&version_id=0&hasexp=0&opec=0&opov=0&opcsrf=0&opfileinc=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opginf=0&opdos=0&orderby=1&cvssscoremin=10" width="100%" height="300px"></iframe> -->
</div>
<div class="demo2" id="demo2"><p></p></div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
var FEED_URL='http://www.cvedetails.com/json-feed.php?numrows=10&vendor_id=0&product_id=0&version_id=0&hasexp=0&opec=0&opov=0&opcsrf=0&opfileinc=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opginf=0&opdos=0&orderby=1&cvssscoremin=10';

$.ajax({
  url      :  FEED_URL + encodeURIComponent(FEED_URL),
  dataType : 'json',
  success  : function (data) {
    if (data.responseData.feed && data.responseData.feed.entries) {
      $.each(data.responseData.feed.entries, function (i, e) {
        header('Access-Control-Allow-Origin: http://www.cvedetails.com');
        alert('e.title');
        $('#demo2').append($('<p/>').text(e.title));

        console.log("------------------------");
        console.log("title      : " + e.title);
        console.log("author     : " + e.author);
        console.log("description: " + e.description);
      });
    }
  }
});
});
</script>
</body>
</html>

`

enter image description here

1 个答案:

答案 0 :(得分:0)

看起来该网站不支持CORS,因为他们没有设置适当的Access-Control-Allow-Origin。由于您无法修复此客户端(在黑客之外),因此最好设置一个代理服务器来代理对该API的调用。代理服务器可以位于您自己的域中,也可以指定自己的CORS响应头。您还可以查看JSONP。

CORS指南:https://www.moesif.com/blog/technical/cors/Authoritative-Guide-to-CORS-Cross-Origin-Resource-Sharing-for-REST-APIs/