javascript - Parse json & html data from ajax response - Stack Overflow

I'm sending ajax request to url and getting following response:Ajax Request:<div id="htmld

I'm sending ajax request to url and getting following response:

Ajax Request:

<div id="htmldata"></div>
<script type="text/javascript">
    jQuery.ajax({
        type: "GET",
        url: "/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.

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.

I'm sending ajax request to url and getting following response:

Ajax Request:

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

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.

Share Improve this question asked Sep 10, 2014 at 7:34 yogesh suhagiyayogesh suhagiya 4982 gold badges6 silver badges20 bronze badges 10
  • Is this json string always on the last line of your response? – Stijn Bernards Commented Sep 10, 2014 at 7:37
  • yes it always in last line, but json data may be different. – yogesh suhagiya Commented Sep 10, 2014 at 7:37
  • var responsetext = response.split("\n"); alert(responsetext[responsetext.length -1]); Try this Or you could use javascripts last index of. – Stijn Bernards Commented Sep 10, 2014 at 7:39
  • i suggest just stick to one, which is JSON, just add another inside: data: "rest of markup html" – Kevin Commented Sep 10, 2014 at 7:40
  • 1 html code is generated through view file – yogesh suhagiya Commented Sep 10, 2014 at 7:43
 |  Show 5 more ments

3 Answers 3

Reset to default 3

Make you json object like this:

$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,
Now Javascript Turn:

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

In the parsing part, you could test if the response is json via trying to parse it and get results. If the response is not json, then print out the html response. I would suggest you to make a specific element for hosting the dynamic html, which es from your AJAX call.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742343606a4426104.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信