![](/img/trans.png)
[英]How to click a hyperlink(<a> tag) automatically by clicking only a submit button in html?
[英]REST API - Automatically make requests by clicking submit
我有這個腳本,但是當我單擊“提交”按鈕時,我不知道如何自動運行它
<script type="text/javascript">
var cbIntegrationId = "xxxxxx";
var clientId = "xxxxx";
var clientSecret = "xxxxxxx";
var tableName = "xxxxxx";
//Get access token
$.post(
"https://" + cbIntegrationId + ".caspio.com/oauth/token",{
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret
},
function(cbAuth){
//Run POST call
$.ajax({
url: "https://" + cbIntegrationId + ".caspio.com/rest/v2/tables/" + tableName + "/records?response=rows",
type: 'POST',
'data': JSON.stringify({"UniqueID":"34","Full_Name": "Name_Value"}), //Define record values
headers: {
"Authorization": "Bearer " + cbAuth.access_token, //Extracts the access token from the initial authorization call
"Content-Type": "application/json", //Required, otherwise 415 error is returned
"Accept": "application/json"
},
dataType: 'json',
success: function (data) {
console.log(data.Result); //Check the console to view the new added row
},
error: function(data) {
console.log(data.responseJSON); //Check the console to view error message if any
}
});
}
);
</script>
這是表格
<form method="" action="">
Full Name:
<input type="text" name="full_name" id="full_name"><br>
<input type="submit" value="Send">
</form>
我想知道如何自動運行腳本並獲得成功消息(對或錯)
嘗試這個:
<form method="" action="" onSubmit="callApi()">
Full Name:
<input type="text" name="full_name" id="full_name"><br>
<input type="submit" value="Send">
</form>
function callApi() {
//Get access token
$.post(
"https://" + cbIntegrationId + ".caspio.com/oauth/token",{
grant_type: "client_credentials",
client_id: clientId,
client_secret: clientSecret
},
function(cbAuth){
//Run POST call
$.ajax({
url: "https://" + cbIntegrationId + ".caspio.com/rest/v2/tables/" + tableName + "/records?response=rows",
type: 'POST',
'data': JSON.stringify({"UniqueID":"34","Full_Name": "Name_Value"}), //Define record values
headers: {
"Authorization": "Bearer " + cbAuth.access_token, //Extracts the access token from the initial authorization call
"Content-Type": "application/json", //Required, otherwise 415 error is returned
"Accept": "application/json"
},
dataType: 'json',
success: function (data) {
console.log(data.Result); //Check the console to view the new added row
},
error: function(data) {
console.log(data.responseJSON); //Check the console to view error message if any
}
});
}
);
}
</script>
也許您需要更改UniqueID
和Full_Name
。 如果Full_Name是表單上的用戶輸入,則可以這樣獲得:
$('input[name="full_name"]').val()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.