2 回答
TA貢獻2080條經驗 獲得超4個贊
您需要設置處理程序來調用您的函數,而不僅僅是分配:
<button onclick="createSession()">Submit</button>
此外,您可以在createSession函數中創建 url:
function createSession() {
var ip = document.getElementById("apigatewayip").value;
var _url = "https://" + ip + "/api/v0/sessions"
console.log(_url) // Use this for ajax call
/*$.ajax({
"url": _url,
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({
"gatewayVersion": "1.00.1.15"
}),
});*/
}
<h1>Making AJAX Rest Calls</h1>
<div class="custom-select" style="width:200px;">
<select name="selectip" id="apigatewayip">
<option value="" disabled selected>API Gateway IP</option>
<option value="169.254.1.10">169.254.1.10</option>
</select>
<button onclick="createSession()">Submit</button>
</div>
TA貢獻1828條經驗 獲得超13個贊
等待文檔使用文檔就緒$(function() { })并使用 jQuery 呈現,不要與舊的 JavaScript 選擇混合
使用done回調而不是success
//Global function 1
function makeUrl() {
var ip = $("#apigatewayip").val();
return "https://" + ip + "/api/v0/sessions"
};
//Global function 2
function createSession(_url) {
return $.ajax({
"url": _url,
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({
"gatewayVersion": "1.00.1.15"
}),
});
}
//Wait until document fully render
$(function() {
//After document render call the methods
createSession(makeUrl());
createSession(makeUrl()).done(function() {
});
});
添加回答
舉報
