我不是很喜欢Javascript。我主要用python做后端。这次我别无选择,只能做javascript:P
我遇到了XMMHttpRequest响应问题。 我有这个HTML
<div>
<h3>Drop in orders</h3>
<hr>
<button onclick="triggerScenario(this);" class="btn btn-default" id="z-drop-sev1">
<img src={{ url_for('static', filename='images/250.jpeg' ) }} class="img-responsive" alt="logo">
</button>
<button onclick="triggerScenario(this);" class="btn btn-default" id="6-drop-sev1">
<img src={{ url_for('static', filename='images/250.jpeg' ) }} class="img-responsive" alt="logo">
</button>
<button onclick="triggerScenario(this);" class="btn btn-default" id="z-6-drop-sev1">
<img src={{ url_for('static', filename='images/250.jpeg' ) }} class="img-responsive" alt="logo">
</button>
</div>
当用户单击其中一个按钮时,将调用此函数:
function triggerScenario(clickedButton) {
var jiraEndpoint = "/api/jira/create/"+clickedButton.id;
var xmattersEndpoint = "/api/dmx/trigger/";
var zitEndpoint = "/api/zit/"+clickedButton.id+"/";
var ticket;
var id;
function createTicket() {
var httpRequest = new XMLHttpRequest();
var data = "{"+
"\"project\": {"+
"\"id\": 10150"+
"},"+
"\"issuetype\": {"+
"\"name\": \"Incident\""+
"},"+
"\"priority\": {"+
"\"name\": \"high\""+
"}"+
"}"
var jsonData = JSON.parse(data);
httpRequest.open('POST',jiraEndpoint);
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = JSON.parse(this.responseText);
ticket = response.number;
//Here I see the value I need
console.log(ticket)
httpRequest = null;
return ticket;
}
}
};
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.send(data);
};
ticket = createTicket();
//This gives me "undefined"
console.log("Ticket: "+ticket);
//sendZit(ticket);
//triggerCall();
}
问题是“票证”的值始终未定义。如果我在函数内部记录值,它会显示我需要的内容。有更多JS知识的人能解释我这里发生了什么吗?提前致谢!