提问人:Anurag Dixit 提问时间:6/2/2023 更新时间:6/2/2023 访问量:23
尝试在应用程序内部发布数据
Trying to Post the data internally in the application
问:
我正在尝试从应用程序本身发布数据,我们有一个应用程序,我们可以根据您的要求使用 html 进行一些自定义,所以我使用 JavaScript 和 Ajax 调用在 html 中创建了一个示例,但脚本没有按预期工作。
下面是 html 和脚本,请看一下:
一旦详细信息更新,我还想向用户确认一条消息。同样,“详细信息已提交!
<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>```
答:
0赞
classicjonesynz
6/2/2023
#1
你的问题是你试图将你的DATA作为JSON字符串传递;当它应该是 JSON 对象时。
您需要在客户端或服务器上解析 JSON
PHP的
<?php
print_r(json_decode($_REQUEST['citype'], true));
?>
爪哇岛
Gson gson = new Gson();
gson.fromJson(value, type);
JavaScript的
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)
[HTML全文]
<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>
评论
data
"citype": {
JSON.stringify