简体   繁体   English

创建 Jira 问题的 PHP 表单不起作用

[英]PHP Form to create a Jira issue is not working

I am a beginner regarding working with php and api.我是使用 php 和 api 的初学者。 What I am trying to do is create a new Jira issue using the Jira Rest Api.我想要做的是使用 Jira Rest Api 创建一个新的 Jira 问题。 It is my first project of this type.这是我第一个此类项目。 I will post bellow the code from my 2 pages.我将发布我的 2 页中的代码。 I can't figure out what is my problem.我无法弄清楚我的问题是什么。 I mention I already tested the api using Advanced REST client (basically same tool as POSTMAN) and when I do it there it works, on my site it doesn't.我提到我已经使用高级 REST 客户端(与 POSTMAN 基本相同的工具)测试了 api,当我在那里进行测试时,它可以工作,但在我的网站上却没有。 I mention that "JIRA PLACEHOLDER" is replaced by the actual jira instance in my code.我提到“JIRA PLACEHOLDER”在我的代码中被实际的 jira 实例替换。

Jira-create-issues.php Jira-create-issues.php

<?php
    $base64_usrpwd = base64_encode($_POST['user'].':'.$_POST['pass']);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://JIRA-PLACEHOLDER/jira/rest/api/2/issue/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
                                                'Authorization: Basic '.$base64_usrpwd)); 

    $arr['project'] = array( 'key' => 'TEST');
    $arr['summary'] = $_POST['summary'];
    $arr['description'] = $_POST['description'];
    $arr['issuetype'] = array( 'name' => $_POST['type']);

    $json_arr['fields'] = $arr;

    $json_string = json_encode ($json_arr);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$json_string);
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;
?>

And the code for jira-create-issue.html:以及 jira-create-issue.html 的代码:

<html>
<head>
<script src="jquery-2.1.4.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div id="wrapper">
    <h1>Create Issue</h1>
    <form id="create-form">
        Summary: <input type="text" name="summary" id="summary" value=""/>
        Description: <input type="text" name="description" id="description" value="" />
        Issue Type: <input type="text" name="type" id="type" value=""/>
        Username: <input type="text" name="user" id="user" value=""/>
        Password: <input type="password" name="pass" id="pass" value=""/>
        <input type="button" id="button" value="Create Issue"/>
    </form>
</div>
<script>
$('#button').click(function() {
     $.ajax({
       type: "POST",
       url: "jira-create-issue.php",
       data: $('#create-form').serialize(),
       success: function(data){
          alert(data);
       },
       dataType: "html"
    });
});
</script>
</body>
</html> 

Update: After looking at your code more deeply, it's working as it should... One thing you can try is to publish it to a server.更新:更深入地查看您的代码后,它可以正常工作......您可以尝试的一件事是将其发布到服务器。 It could be that Jira doesn't allow you calling them from your localhost.可能是 Jira 不允许您从本地主机调用它们。 That's all I could do.这就是我能做的。 Your front end works (tested it, also without my additions. Obviously style.css and jquery needs to be properly referenced).你的前端工作(测试它,也没有我的添加。显然 style.css 和 jquery 需要正确引用)。 Your backend seems correct, it takes the fields and maps them correctly.您的后端似乎是正确的,它获取字段并正确映射它们。 So the error must be in that Jira doesnt respond on the endpoint you are calling them on...good luck!所以错误一定是因为 Jira 没有在你调用它们的端点上响应......祝你好运!

No submit button?没有提交按钮? (type=submit) (类型=提交)

Change改变

<input type="button" id="button" value="Create Issue"/>

to

<input type="submit" id="button" value="Create Issue"/>

If this is not working, try looking at your console for JavaScript errors?如果这不起作用,请尝试查看您的控制台是否有 JavaScript 错误? Especially at the POST.尤其是在POST。

You also forgot the form action.你也忘记了表单操作。 Try adding that ;-)尝试添加;-)

<form id="create-form" action="Jira-create-issues.php" method="POST">

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM