1

我想创建一个带有复选框的动态地图来自定义地图上的标记。当然,我为此使用 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?

4

4 回答 4

0
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);
}

这应该更好。

于 2013-09-25T11:21:28.043 回答
0

请试试这个

$( "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);
}

我希望这能帮到您。

于 2013-09-25T11:31:36.597 回答
0

在输出 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);  
    }
于 2013-09-25T11:36:38.790 回答
0
 $( "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
     }
    });
于 2013-09-25T11:10:06.827 回答