【问题标题】:Display Object in aspx page在 aspx 页面中显示对象
【发布时间】:2017-09-30 04:24:11
【问题描述】:

我正在使用 web 服务 GetEmployeebyId,我得到了对象数据,我想在 aspx 页面中使用 javascript 将其显示在列表中。

请帮帮我!!

这是我的代码:它错过了显示对象的函数

**

<script>
    $(document).ready(function () {
        $.ajax({
            type: 'GET',
            url: _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/EmployeeService.svc/GetEmployeebyNom/kmjdfb', // Location of the service
            contentType: 'application/json; charset=utf-8', // content type sent to server
            processdata: true,
            success: function (msg) {
            datasource:msg
            }
        });
    });
        </script>**

在这张图片中找到了我的对象“msg”

【问题讨论】:

  • 据我所知,jQuery 没有任何内置函数来转储 JavaScript 变量,可能有两个原因:1)所有体面的浏览器都已经在他们的开发者工具中实现了这样的功能 2)完全不相关最终用户。是什么阻止您构建格式良好的 HTML 表示?

标签: javascript jquery json ajax jquery-ui


【解决方案1】:

GetEmployeeByNom 函数必须显示员工详细信息,然后使用您当前的对象:

{
    "Adresse": "hcs",
    "CIN": 516515,
    "Competence": "chc",
    "Contract": null,
    "Date_naissance": "Date(1490770800000-0700)/",
    "Email": "jbmkjb@hotmail.fr",
    "Etat_civil": "$Resources:TravelCasrdsFields,Single;",
    "Job_Title": "csv",
    "Nationalite": "hsvcsg",
    "Nom": "sdjhvc",
    "Prenom": "kmjdfb",
    "Sexe": "Mr",
    "Telephone": 65465
}

你应该试试这样的:

(function() {

  var msg = {
    "Adresse": "hcs",
    "CIN": 516515,
    "Competence": "chc",
    "Contract": null,
    "Date_naissance": "Date(1490770800000-0700)/",
    "Email": "jbmkjb@hotmail.fr",
    "Etat_civil": "$Resources:TravelCasrdsFields,Single;",
    "Job_Title": "csv",
    "Nationalite": "hsvcsg",
    "Nom": "sdjhvc",
    "Prenom": "kmjdfb",
    "Sexe": "Mr",
    "Telephone": 65465
  };

  // Include this function in your code.
  function displayEmployee(msg) {
    var ulList = "";
    ulList += "<ul>";
    for (var property in msg) { // For every property in the msg object.
      if (msg.hasOwnProperty(property)) { // Checks if the property exists.
        ulList += "<li><span>";
        ulList += property; // Gets the property name.
        ulList += "</span>: ";
        ulList += msg[property]; // Gets the property value.
        ulList += "</li>";
      }
    }
    ulList += "</ul>";
    return ulList; // Returns the ul tag with the data.
  }

  // Include this line in your success: function(msg) {} part.
  document.getElementById("EmployeeDetail").innerHTML = displayEmployee(msg);
})();
#EmployeeDetail ul {
  border: solid 1px #97bcd6;
  list-style-type: none;
  margin: 0;
  padding: 0;
}

#EmployeeDetail ul li {
  margin: 10px;
}

#EmployeeDetail ul li span {
  font-weight: bold;
}
<div id="EmployeeDetail">

</div>

然后,在您添加的代码中添加document.getElementById("EmployeeDetail").innerHTML = displayEmployee(msg);

$(document).ready(function() {
  $.ajax({
    type: 'GET',
    url: _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/EmployeeService.svc/GetEmployeebyNom/kmjdfb', // Location of the service
    contentType: 'application/json; charset=utf-8', // content type sent to server
    processdata: true,
    success: function(msg) {
      document.getElementById("EmployeeDetail").innerHTML = displayEmployee(msg);
    }
  });
});

【讨论】:

  • 不客气,Raf。如果您需要更多帮助,请随时询问。不要忘记接受这个答案。 :)
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多