javascript 尝试在应用程序内部发布数据

hec6srdp  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(272)

我试图从应用程序本身发布数据,我们有一个应用程序,我们有能力使用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>```
zaq34kh6

zaq34kh61#

您的问题是您试图将DATA作为JSON字符串传递;当它应该是JSON对象时。
您需要在客户端或服务器端解析JSON
PHP

<?php

print_r(json_decode($_REQUEST['citype'], true));

?>

java

Gson gson = new Gson();
gson.fromJson(value, type);

简体中文

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>

相关问题