簡體   English   中英

檢索 JSON 的 AJAX 調用 - 如何將結果格式化以進行篩選

[英]AJAX Call to Retrieve JSON - How to format results to screen

我創建了一個自動發布 3 個標識字段(用於身份驗證)以檢索 JSON 結果的文件。 它工作正常(現在作為概念證明開發,因此 id 是硬編碼的)。 成功后,JSON 作為警報返回給瀏覽器。

如何在屏幕上返回和格式化 JSON 結果?

這是工作網址: https : //www.advantageengagement.com/REST/js_yes.html

<!DOCTYPE html>
<html>
<head>
     <title>Javascript POST Form</title>
     <meta charset="utf-8">
</head>
<body>
    <script type="text/javascript">
        var http = new XMLHttpRequest();
        var postdata= "id_eap=999&id_company=&password=AAA111BBB2";              
        http.open("POST", "https://www.advantageengagement.com/REST/content/read.php", true);
        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.onreadystatechange = function() {
             if(http.readyState == 4 && http.status == 200) {
                 alert(http.responseText);    
             }
        }
        http.send(postdata);
     </script>
</body>
</html>

如果我明白你的問題,

根據您希望如何顯示數據,您可以將警報中顯示的數據拆分為“,”,並將這些新的單個項目中的每一個用作帶有 JavaScript DOM 操作的編號/無序列表的數據。

以下是有關拆分數據的信息:語法 split(separator, limit)

Parameter
separator:  The character to separate the string. The separator itself is 
a string. If the separator is not present it returns the entire string. in 
your case you could use ",". 

limit : An integer specifying a limit on the number of substrings to be 
found. Make it big if you don't know how many individual items are in the 
object showing in your alert.

以下是有關 javascript 的信息: https : //www.w3schools.com/js/js_htmldom_html.asp

您還沒有完全指定,您想如何格式化 JSON,但我試了一下並相應地做了。

您可以使用以下代碼。

http.onreadystatechange = function() {
             if(http.readyState == 4 && http.status == 200) {
             var a=http.responseText.split(" ");
            a.forEach((e)=> $('body').append(e + "<br>"))

             }
        }

split 函數將JSON拆分為每個(" ")的數組。 您可以在此處放置與要拆分的任何參數相關的任何參數。 然后用換行符附加數組中的每個元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM