2016-08-18 54 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