简体   繁体   中英

How to remove all HTML from JSON Response

I use json for php javascript . But when javascript call back , it show HTML Every Where .

Code:

$('#buyNow').click(function(){
    $('#loading').html('<i class="fa fa-spinner fa-pulse fa-3x fa-fw margin-bottom"></i>');
    $('#group-pay').hide();
    var nameproduct = $('#myModalLabel1').text();
    $.get(base_url+"dashboard/shop",{ buy: idProduct },function(data){
        var obj = $.parseJSON(data);
        alert(data);
        if(obj.Code == 200){
            $('#loading').html('<div class="alert alert-success">Success <strong>'+nameproduct+'</strong></div>');
            $('#group-pay').show();
        }else{
            $('#loading').html('<div class="alert alert-danger">Error</div>');
            $('#group-pay').show();
        }
    });
});

And my PHP:

$vatpham = $_REQUEST['buy'];
if($vatpham >= 400 && $vatpham <= 609)
{
    $json = array("Code" => "200");
    echo json_encode($json);
}

Error Reponse Show is:

{"Code":"200"}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cửa hàng</title>

<link href="http://bieberkieu.com/project/demo/css/bootstrap.min.css" rel="stylesheet">
<link href="http://bieberkieu.com/project/demo/css/datepicker3.css" rel="stylesheet">
<link href="http://bieberkieu.com/project/demo/css/styles.css" rel="stylesheet">
<link href="http://bieberkieu.com/project/demo/css/styles.css" rel="stylesheet">
<!--Icons-->
<script src="http://bieberkieu.com/project/demo/js/lumino.glyphs.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->

</head>

<body>

It show my html behind json response , any want know why ? i try ob_clean() but not working .

In codeigniter controller class, create one method to only have a code producing json and returning it, and never load any view from that method :

// dashboard/shop
public function shop () {
  // in this method should only returned jSON respond
  // instead of any view
  $vatpham = $_REQUEST['buy'];
  if($vatpham >= 400 && $vatpham <= 609)
  {
     $json = array("Code" => "200");
     echo json_encode($json); //<--- only return this
  }
  // remove below code if have, or create another method to only return
  // json respond
  $this->load->view('somefilename', $data);

}

Or if you cant remove that, you need to replace all the html content and leave behind JSON, but this solution are not wise :

$.get(base_url+"dashboard/shop",{ buy: idProduct },function(data){
    // remove all the HTML content that start with `<` till the end
    var obj = JSON.parse( data.replace( /\<.+/ig,'') ); // or $.parseJSON( data.replace( /\<.+/ig,'') );
    ........
    ........
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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