У меня проблема, когда я использую 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.
Не могли бы вы предоставить нам ответ AJAX NSE? – James
мой ответ AJAX - это строка с содержимым всех элементов html на странице –