我正在嘗試從應用程序本身發布數據,我們有一個應用程序,我們可以根據您的要求使用html進行一些定制,所以我使用JavaScript和Ajax調用創建了一個html示例,但腳本沒有按預期工作。
下面是html和腳本,請看看:
我還想向用戶確認一條消息,一旦細節將被更新。同樣地& quot詳情已提交!"
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.button {
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
box-shadow: 0px 0px 2px 2px rgb(0,0,0);
}
.button1 {background-color: #071e26;} /* Dark Blue*/
.button1:hover {
background-color:#e9f5f9;
transition: 0.2s;
color: #071e26;
}
.button2 {background-color: #008CBA;} /* Blue */
</style>
</head>
<body>
<h1>User Acknowledgement</h1>
<p>Please acknowledge below if you recieved a laptop</p>
<button id=AssetAmt class="button button1" onClick="ajaxCall()">Please acknowledge</button>
<script>
function ajaxCall() {
$.ajax({
type: "POST",
url: "xyz.com/api",
data: `"citype": {
"name": "Windows Workstation",
"criterias": {
"criteria": {
"parameter": {
"name": "CI Name",
"value": "xyz.dummy.com"
}
}
},
"newvalue": {
"record": {
"parameter": [
{
"name": "Model",
"value": "Model 2023"
},
{
"name": "Location",
"value": "Optional"
}
]
}
}
}`,
success: function (result) {
//alert('Information Updated');
console.log(result);
},
dataType: "json"
});
}
ajaxCall();
</script>
</body>
</html>```
您的問題是您試圖將數據作為JSON字符串傳遞;當它應該是JSON對象時。
您需要在客戶機或服務器上解析JSON
服務器端編程語言(Professional Hypertext Preprocessor的縮寫)
<?php
print_r(json_decode($_REQUEST['citype'], true));
?>
Java 語言(一種計算機語言,尤用于創建網站)
Gson gson = new Gson();
gson.fromJson(value, type);
Java Script語言
var jsonText = $("div#myDataBlock").text().trim();
var jsonObject = JSON.parse(jsonText);
console.log('jsonText ', jsonText);
console.log('jsonObject', jsonObject);
var citypeObject = {};
citypeObject['citype'] = jsonObject;
var successMethod = function(result) {
// alert('Information Updated');
console.log('result', result);
};
console.log('citypeObject', citypeObject);
var ajaxOptions = {
"url": "xyz.com/api",
"type": "POST",
"data": citypeObject,
"success": successMethod
};
$.ajax(ajaxOptions)
超文本標記語言
<div id="myDataBlock" style="display:none;" >
{"name":"Windows Workstation","criterias":{"criteria":{"parameter":{"name":"CI Name","value":"xyz.dummy.com"}}},"newvalue":{"record":{"parameter":[{"name":"Model","value":"Model 2023"},{"name":"Location","value":"Optional"}]}}}
</div>