Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以编程方式使用ajax jQuery美化JSON响应_Javascript_Jquery_Html_Json_Ajax - Fatal编程技术网

Javascript 以编程方式使用ajax jQuery美化JSON响应

Javascript 以编程方式使用ajax jQuery美化JSON响应,javascript,jquery,html,json,ajax,Javascript,Jquery,Html,Json,Ajax,如何格式化下面的JSON并显示在html页面中 [ { "customerAddressZip": "", "customerAddressState": "", "referenceNumber": "APP-0911-1004-32", "dateServiceCompleted": "", "claimParts": [ { "causal_part": "true", "failedPartNumber": "1112", "partInvoiceNumber": "", "partQu

如何格式化下面的JSON并显示在html页面中

[ { "customerAddressZip": "", "customerAddressState": "", "referenceNumber": "APP-0911-1004-32", "dateServiceCompleted": "", "claimParts": [ { "causal_part": "true", "failedPartNumber": "1112", "partInvoiceNumber": "", "partQuantity": "1", "failedPartSerial": "", "partSequence": "1", "partAmount": "", "partSerialNumber": "", "failedPartQuantity": "1", "Claim_WID": "20", "failedPartInstallDate": "", "partNumber": "" } ], "serialNumber": "111", "customerComplaint": "", "customerEmail": "", "applicationType": "", "customerPhone": "", "warrantyType": "01", "customerLastName": "", "customerAddressLine1": "", "serviceAgreementNumber": "", "customerAddressLine2": "", "customerFirstName": "", "retrievalCode": "BMLExmiaYkaj", "repairCategory": "", "claimStatus": "", "competitiveEquipment": "f", "dateServiceRequested": "", "serviceExplanation": "", "customerAddressCity": "", "purchaseDate": "", "Claim_WID": "20", "defectCode": "", "modelNumber": "24ABA318A0030010" } ] 
格式化JSON

[{

    "customerAddressZip": "",
    "customerAddressState": "",
    "referenceNumber": "APP-0911-1004-32",
    "dateServiceCompleted": "",
    "claimParts": [
        {
            "causal_part": "true",
            "failedPartNumber": "1112",
            "partInvoiceNumber": "",
            "partQuantity": "1",
            "failedPartSerial": "",
            "partSequence": "1",
            "partAmount": "",
            "partSerialNumber": "",
            "failedPartQuantity": "1",
            "Claim_WID": "20",
            "failedPartInstallDate": "",
            "partNumber": ""
        }
    ],
    "serialNumber": "111",
    "customerComplaint": "",
    "customerEmail": "",
    "applicationType": "",
    "customerPhone": "",
    "warrantyType": "01",
    "customerLastName": "",
    "customerAddressLine1": "",
    "serviceAgreementNumber": "",
    "customerAddressLine2": "",
    "customerFirstName": "",
    "retrievalCode": "BMLExmiaYkaj",
    "repairCategory": "",
    "claimStatus": "",
    "competitiveEquipment": "f",
    "dateServiceRequested": "",
    "serviceExplanation": "",
    "customerAddressCity": "",
    "purchaseDate": "",
    "Claim_WID": "20",
    "defectCode": "",
    "modelNumber": "24ABA318A0030010"
}]
AJAX代码:

function addData() {

            $.ajax({
                url: requrl,
                crossDomain: true,
                dataType: "json",
                success: onSuccess,
                error: onError
            });

            function onSuccess(data, textStatus, jqXHR) {
                var response = JSON.stringify(data, null, "\t");
                $('#apidata').html("<h2>Claim Found</h2>\n");
                $('#apidetails').html("<p>Status:" + textStatus + "</p>\n" + "<p>Data:</p>\n" + "<p>" + response + "</p> " + "\n");
            }

            function onError(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR);
                $('#apidata').html("<h2>No Claim Found</h2>\n<p>Please validate you Retrieval Code and ServiceBench credentials.</p>\n");
                $('#apidetails').html("<h2>Error!</h2>\n<p>jqXHR:</p>\n" + jqXHR + "\n<p>Status:</p>\n" + textStatus + "<p>Error:</p>\n" + errorThrown);
            }
        }
此处requrl返回上述json响应。

尝试:

$('#apidetails').html("<p>Status:" + textStatus + "</p>\n" + "<p>Data:</p>" + "<pre>" + response + "</pre>");

标记保留格式。不需要在HTML中定义换行符。

我这样做了,但它仍然不能像上面提到的格式化JSON那样工作。您可以看到ajax代码中的更改,将JSON文本包装在和标记中感谢您的工作@Iceburg听到这句话很高兴: