2016-08-18 39 views
1

我有问题是当我使用ajax发送数据到php服务器,并让它显示在视图中,但数据不能显示在视图中,虽然它加载时,我检查控制台。请帮我解决它。 这是我的代码:
查看和脚本尽管装载了元素,但不能显示元素?

<div class="col-xs-12" style="display: block; margin-bottom: 10px;"> 
<h5 class="col-sm-3 col-sm-offset-1 control-label" style="">Vị trí:</h5> 
<div id="positionDiv" class="col-sm-3"> 
    <select id="position" tabindex="9" name="recruitment[positions]" value="<?php echo $this->data['recruitment']->positions; ?>" data-none-selected-text class="selectpicker"> 
     <option style="display: none" selected disabled value=""></option> 
     <?php foreach($this->data['position'] as $key => $value): ?> 
      <option value="<?php echo $value->positions ?>" <?php if ($this->data["recruitment"]->positions == $value->name): ?>selected<?php endif ?>> 
       <?php echo $value->name; ?> 
      </option> 
     <?php endforeach ?> 
    </select> 
    <input id="positionHidden" type="hidden"> 
</div> 
<script> 
    $(document).ready(function() { 
     $("#position option").each(function(){ 
      $(this).siblings("[value='"+ this.value+"']").remove(); 
     }); 
     $('#market').on('change', function(event) { 
      var value = $('#market').val(); 
      $.ajax({ 
       url: window.location.href, 
       type: 'POST', 
       data: { 
        market: value 
       }, 
       success: function(response){ 
        var indexStart = response.indexOf('id="position"') - 8; 
        var indexEnd = response.indexOf('positionHidden') - 49; 
        var str = response.substring(indexStart,indexEnd).replace(/\s\s+/g, ' '); 
        $('#position').remove(); 
        $('#positionDiv').append(str); 
        //document.getElementById('positionDiv').innerHTML = str; 
        console.log($('#positionDiv')); 
       } 
      }); 

     }); 
     $.ajaxPrefilter(function(options, originalOptions, jqXHR) { 
      options.async = true; 
     });     
    }); 
</script> 

控制器

class CandidateController extends BaseController { 

    public function __construct($route, $urlValue) { 
     parent::__construct($route, $urlValue); 
    } 

    public function initData() { 
     $this->view->setData("resource", ResourceCompany::getAllResource()); 
     $this->view->setData("market", Market::getAllMarket());   
    } 

    public function showPosition() { 
     $market = $_POST['market']; 
     isset($market) ? $market : null; 

     $this->view->setData("position", Recruitment::getPositionByMarket($market));   
    } 

    public function showTitle() { 
     isset($market) ? $market : null; 
     isset($position) ? $position : null; 

     $this->view->setData("title", Recruitment::getTitleByMarketPosition($market, $position)); 
    } 

    public function register() { 
     $this->initData();  

     if (isset($_POST['market'])) { 
      $this->showPosition(); 
     } 
    } 
    $this->view->output("menu"); 
} 

一切都好,数据发送和得到的是好的,但它无法显示虽然我可以从console.log得到它。

+0

你能分享我们你的AJAX respo NSE? – James

+0

我的AJAX响应是字符串,其内容为页面 –

回答

0

mybe你在你的内容中有特殊字符。 您在发送php代码之前最多替换此字符,并在获取后返回javascript

“&”= & amp;

“\”” = & QUOT;

“”“= &者;

”<“= & LT;

”>“= & GT;

删除空间后& plase

+0

的所有html元素,当我添加到div标签时,这是我的'str':' –

+0

在您的内容中是特殊字符,例如<," ,>。在PHP代码中替换特殊字符与等效代码以及在ajax中获取字符串时(客户端)原始字符,然后将您的字符串添加到div内容。 – Masoomian