我想创建一个带有复选框的动态地图来自定义地图上的标记。当然,我为此使用Ajax,我想使用它:)
我的 ajax 调用没问题,我通过以下方式将数据从视图传递到控制器:
$( "input[type=checkbox]" ).click(function() {
var data = $(this).val();
var request = $.ajax({
type: "POST",
url: "<?php echo site_url(current_url());?>",
data: "categorie=" + data,
});
request.done (function(data){
var json = data;
// Remove the markers and add new ones
我将数据传递给控制器并像这样处理它
if ($this->input->is_ajax_request()) {
$category = $_POST['categorie'];
$unsigned_url = "http://api.yelp.com/v2/search?location=" . $data->home['city']['cities_name'] . "&category_filter=" . $category;
$new_JSON = $this->listing_lib->getJsonFromYelp($unsigned_url);
echo $new_JSON;
}
问题是,在 JS 的 JSON 变量中,我从 PHP 中获得了 JSON,但我也得到了页面的所有 DOM?为什么呢?我怎样才能只得到我的 JSON ?
$( "input[type=checkbox]" ).click(function() {
var data = $(this).val();
$.ajax({
type: "POST",
url: "<?php echo site_url(current_url());?>",
data: {"categorie":data},
dataType:"json",
cache: false
success: function (categorie) {
/// your code for show category
}
});
if ($this->input->is_ajax_request()) {
$category = $_POST['categorie'];
$unsigned_url = "http://api.yelp.com/v2/search?location=" . $data->home['city']['cities_name'] . "&category_filter=" . $category;
$new_JSON = $this->listing_lib->getJsonFromYelp($unsigned_url);
header('Content-type: application/json');
print_r json_encode($new_JSON);
}
这应该更好。
请尝试这个
$( "input[type=checkbox]" ).click(function() {
var data = $(this).val();
var request = $.ajax({
type: "POST",
url: "<?php echo site_url(current_url());?>",
data: "categorie=" + data,
dataType:"json",
success:function(response){
alert(response);
}
});
和控制器
if ($this->input->is_ajax_request()) {
$category = $_POST['categorie'];
$unsigned_url = "http://api.yelp.com/v2/search?location=" . $data->home['city']['cities_name'] . "&category_filter=" . $category;
$new_JSON = $this->listing_lib->getJsonFromYelp($unsigned_url);
print json_encode($new_JSON);
}
我希望这对你有帮助。
在输出 json 之前,请尝试设置输出类型和输出:
if ($this->input->is_ajax_request()) {
$category = $_POST['categorie'];
$unsigned_url = "http://api.yelp.com/v2/search?location="
.$data->home['city']['cities_name'] . "&category_filter=" . $category;
$new_JSON = $this->listing_lib->getJsonFromYelp($unsigned_url);
$this->output->set_content_type('application/json');
$this->output->set_output($new_JSON);
}