dssu33392 2016-03-18 02:40
浏览 77
已采纳

如何从JSON响应中删除所有HTML

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 .

  • 写回答

1条回答 默认 最新

  • doufei3152 2016-03-18 02:55
    关注

    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,'') );
        ........
        ........
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组