使用JavaScript模拟通过浏览器中的Web链接获取Jira JSON数据

时间:2015-11-25 19:54:38

标签: javascript jquery json ajax jira-rest-api

我不确定如何提出这个问题。我无法轻松访问我的公司网站以使用普通的AJAX调用来尝试此操作,因此我需要解决跨域问题以及可能更改Jira服务器上的设置。由于我本周一整周都在回家度假,但感恩节前有点无聊,我以为我会玩这个,所以我可以跳过上周五刚刚分配的项目。

使用Jira Rest API,如果我将https://jira.atlassian.com/rest/api/2/project放入浏览器(Chrome)并按回车键,我会在浏览器中返回有效的JSON数据。当然,您必须拥有有效的Jira凭证,并且已经登录到网站上才能使其正常工作(否则您只需获得[]来获得响应),但对于实验,我正在做这个'好的我只想尝试生成一些有效的数据,我可以将其按到表格中。

如何在JavaScript中以自动方式执行此操作,以便返回的JSON进入我可用于后续数据操作的变量?理想情况下,这应该适用于我们真正的Jira网站,但如果它只适用于此问题中链接的网站,我就可以了,因为我说我只是在做一些尝试阅读项目数据和相关属性的实验。

我已经尝试过了(cite):

$.getJSON("https://jira.atlassian.com/rest/api/2/project?callback=?", function(result){
   //response data are now in the result variable
   alert(result);
});

我还尝试将字符串eval()简单地变为变量,但这也不起作用。

我最终的项目很可能是在jQuery / jQuery Mobile中,但直接的JS答案很好,如果有必要,我可以稍后将其翻译成jQuery。

修改

以下是我用来测试此内容的HTML和正在制作的建议,几乎直接来自https://html5boilerplate.com/

<!doctype html>
<html class="no-js" lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="apple-touch-icon" href="apple-touch-icon.png">
    <!-- Place favicon.ico in the root directory -->

    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="js/vendor/modernizr-2.8.3.min.js"></script>
</head>
<body>
    <!--[if lt IE 8]>
        <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Add your site or application content here -->
    <p>Hello world! This is HTML5 Boilerplate.</p>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.3.min.js"><\/script>')</script>
    <script src="js/plugins.js"></script>
    <script src="js/main.js"></script>
    <script type="text/javascript">
        var result = $.get("https://jira.atlassian.com/rest/api/2/project");
        console.log(result);
    </script>
</body>

1 个答案:

答案 0 :(得分:0)

所以使用AJAX / .get等是错误的方法(至少对于这个实验来说,这是我访问JIRA服务器时的正确答案。)

我发现有用的是:

location.href = "https://jira.atlassian.com/rest/api/2/project";
var data = JSON.parse(document.getElementsByTagName('body')[0].innerText);
console.log(data);

这不是一个完美的答案,因为它用JIRA的JSON响应替换了网页的主体,但它让我继续前进。我在this SO post中找到了几种不同的方法。

以下是整个网页:

<!doctype html>
<html class="no-js" lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="apple-touch-icon" href="apple-touch-icon.png">
    <!-- Place favicon.ico in the root directory -->

    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="js/vendor/modernizr-2.8.3.min.js"></script>
</head>
<body>
    <!--[if lt IE 8]>
        <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Add your site or application content here -->
    <p>Get JIRA data</p>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.3.min.js"><\/script>')</script>
    <script src="js/plugins.js"></script>
    <script src="js/main.js"></script>
    <script type="text/javascript">
        location.href = "https://jira.atlassian.com/rest/api/2/project";
        var data = document.getElementsByTagName('body')[0].innerText;
        console.log(data);
    </script>
</body>
</html>

它不能正常工作的原因是你仍然需要追求事实&amp;在控制台中获取数据:

var data = JSON.parse(document.getElementsByTagName('body')[0].innerText);

但是,它会为您提供一组JIRA对象,然后您可以对其进行操作。

在答案中添加this SO question,因为它包含有关解决CORS(跨源资源共享)的有用信息。