简体   繁体   English

从ajax响应中解析json和html数据

[英]Parse json & html data from ajax response

I'm sending ajax request to url and getting following response: 我正在向url发送ajax请求并获得以下响应:

Ajax Request: Ajax请求:

<div id="htmldata"></div>
<script type="text/javascript">
    jQuery.ajax({
        type: "GET",
        url: "http://testing.local/index.php",
        dataType: "html",
        success: function(response) {
            // Parse response here ...
        }
    });
</script>

Response: 响应:

<div class="dataset">
    <h1 class="title">List of Data</h1>                             
    <table width="100%" align="center" class="datatable" >
        <tr>
            <td class="dataField" ><label>Data 1</label></td>
            <td class="dataValue">Value 1</td>
        </tr>
        <tr>
            <td class="dataField" ><label>Data 2</label></td>
            <td class="dataValue">Value 2</td>
        </tr>
        <tr>
            <td class="dataField" ><label>Data 3</label></td>
            <td class="dataValue">Value 3</td>
        </tr>
    </table>
</div>

{"status":"success", "message":"Received data successfully"}

In ajax response, there is both types of data, json and html. 在ajax响应中,有两种类型的数据,json和html。

So I want to alert success or failer message from json data and set html code in div with id "htmldata" using jQuery or javascript. 所以我想从json数据中提醒成功或故障消息,并使用jQuery或javascript在id为“htmldata”的div中设置html代码。

Make you json object like this: 让你像这样的json对象:

$form = '<div class="dataset">
         <h1 class="title">List of Data</h1>                             
         <table width="100%" align="center" class="datatable" >
    <tr>
        <td class="dataField" ><label>Data 1</label></td>
        <td class="dataValue">Value 1</td>
    </tr>
    <tr>
        <td class="dataField" ><label>Data 2</label></td>
        <td class="dataValue">Value 2</td>
    </tr>
    <tr>
        <td class="dataField" ><label>Data 3</label></td>
        <td class="dataValue">Value 3</td>
    </tr>
</table>
</div>';

// Handle Success Message
echo json_encode(array( 'status'=>'success', 
                        'message'=>'Received data successfully', 
                        'html'=>$form));
// Handle Failure Message
/*echo json_encode(array( 'status'=>'fail', 
                        'message'=>'Something went wrong', 
                        'html'=>$form));
*/

In both success and fail situation its return you the form, successfail情况下,它返回你的形式,
Now Javascript Turn: 现在Javascript转:

jQuery.ajax({
    type: "GET",
    url: "http://testing.local/index.php",
    dataType: "json",
    success: function(response) {
        console.log(response.status);
        console.log(response.message);
        console.log(response.html);
    }
});

that's it 而已

This should get you close to what you want 这应该让你接近你想要的

var respJson;
jQuery.ajax({
  type: "GET",
  url : "http://testing.local/index.php"
  dataType : "html",
  dataFilter : function(data, type) {
    var dara = split(data, "\n");
    respJson = jQuery.parseJSON(dara.pop());
    return join(dara);
  },
  success: function(response) {
    // respJson contains your json
    // response is your html
  }
})

Or you could simply include the json as a string in the response header. 或者你可以简单地在响应头中包含json作为字符串。

In the parsing part, you could test if the response is json via trying to parse it and get results. 在解析部分,您可以通过尝试解析响应并获得结果来测试响应是否为json。 If the response is not json, then print out the html response. 如果响应不是json,则打印出html响应。 I would suggest you to make a specific element for hosting the dynamic html, which comes from your AJAX call. 我建议你制作一个特定的元素来托管来自你的AJAX调用的动态html。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM