2017-05-02 75 views
0

我正在使用webservice GetEmployeebyId,我得到的對象數據,我想在aspx頁面中使用JavaScript顯示它。顯示對象在aspx頁面

請幫助我!

這裏我的代碼:它錯過顯示對象

**

<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>** 

我對象「味精」,在這張照片中發現Object details

+0

據我所知,jQuery的沒有任何內置函數來轉儲JavaScript變量,可能有兩個原因:1)所有體面的瀏覽器已經在他們的開發工具中實現了這樣的功能2)它與最終用戶完全無關。什麼阻止你建立一個格式良好的HTML表示? –

回答

0

GetEmployeeByNom功能必須顯示的功能然後與您當前的對象:

{ 
    "Adresse": "hcs", 
    "CIN": 516515, 
    "Competence": "chc", 
    "Contract": null, 
    "Date_naissance": "Date(1490770800000-0700)/", 
    "Email": "[email protected]", 
    "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": "[email protected]", 
 
    "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); 
    } 
    }); 
}); 
+1

謝謝你丹尼,它的工作原理:D –

+0

不客氣Raf。如果您需要更多幫助,請隨時詢問。不要忘記接受這個答案。 :) –