【问题标题】:Ajax function not getting any response from cakephp caontroller functionAjax 函数没有得到 cakephp caontroller 函数的任何响应
【发布时间】:2013-12-20 11:34:40
【问题描述】:

我有一个 jquery 函数如下:

$(document).ready(function() {
    get_search_results();
});

function get_search_results() {
    //get the selected location_id

    var gal_location_id = 0;


    //get the selected package
    var starting_package_wedding = 1;

    var data = "gal_location_id="+gal_location_id+"&starting_package_wedding="+starting_package_wedding;

    var url_to_call = "http://localhost/myshaadi/gal_providers/api_search"; //No alerts
    //var url_to_call = "http://localhost/myshaadi/photographers/gen_uid"; //Alerts 

    $.ajax({
        type: "GET",
        url: url_to_call,
        // async: false,
        //dataType: "json",
        data: data,
        error:function(resp){

        },
        success: function(resp){
            alert(resp);
            //render_search_result(resp);
        }
    });
}

控制器gal_providers函数api_search()是:

function api_search() {
return 'Hello';
}

但是页面并没有提醒任何东西。此外,如果我将变量 url_to_call 更改为其他控制器功能,它会发出警报!所以我想知道错误是什么?

编辑: search.ctp

<style>
#search_filters{
    border: 1px solid silver;
    line-height: 22px;
}
#search_filters .header{
    background-color: orange;
    padding: 5px;
    margin-bottom: 10px;
}
</style>
<script>

$(document).ready(function() {
    get_search_results();
});

function get_search_results() {
    //get the selected location_id

    var gal_location_id = 0;


    //get the selected package
    var starting_package_wedding = 1;

    var data = "gal_location_id="+gal_location_id+"&starting_package_wedding="+starting_package_wedding;

   var url_to_call = "http://localhost/myshaadi/gal_providers/api_search"; //No alerts
    //var url_to_call = "http://localhost/myshaadi/photographers/gen_uid"; //Alerts 
    $.ajax({
        type: "GET",
        url: url_to_call,
        // async: false,
        //dataType: "json",
        data: data,
        error:function(resp){

        },
        success: function(resp){
            alert(resp);
            //render_search_result(resp);
        }
    });
}

</script>
<div id="body-content">
    <ul id="breadcrumbs">
        <li><?php echo $html->link("Home", array("controller" => "pages", "action" => "display"), array("title" => "")); ?></li>
        <li><?php echo $html->link("Gallery", array("controller" => "gallery", "action" => "index"), array("title" => "")); ?></li>
        <li><?php echo $html->link("Photographers", array("controller" => "gallery", "action" => "photographers"), array("title" => "")); ?></li>
        <li>Search</li>
    </ul>

    <div id="container">
        <div style="float:left;width:20%;margin-right:5%">
            <!-- left column -->
            <ul class="search_filters" id="search_filters">
                <li class="header" style="background-color:#dfdfdf">Select City</li>
                <li>
                    <input type="radio" name="gal_location" value="0" checked="checked">
                    All
                </li>
                <?php 
                foreach ($gal_locations as $gal_location) {
                    ?>
                    <li>
                        <input type="radio" name="gal_location" value="<?php echo $gal_location["GalLocation"]["id"]; ?>">
                        <?php echo $gal_location["GalLocation"]["name"]; ?>
                    </li>
                    <?php
                }
                ?>

                <li class="header" style="background-color:#dfdfdf">Select Package</li>
                <li>
                    <input type="radio" name="gal_package" value="0" checked>
                    All Packages
                </li>
                <?php
                for($i = 1; $i < 6;$i++){
                    ?>
                    <li>
                        <input type="radio" name="gal_package" value="<?php echo $i ?>">
                        <?php echo $i ?>
                    </li>
                    <?php
                }
                ?>

            </ul>
        </div>
        <div style="float:left;width:75%">
            <!-- right column -->
            <div class="alert alert-danger">
                Searching ...
            </div>
            <ul id="search_result" class="col-3">
                <?php
                foreach ($gal_providers as $gal_provider) {
                    ?>
                    <li class="card boxcard">
                        <?php echo $html->link($html->image($gal_provider["GalProvider"]["cover_img"],array("class"=>"cover_image")),array("controller"=>"gallery","action"=>"photographer",$gal_provider["GalProvider"]["id"],Inflector::slug($gal_provider["User"]["name"],"-")),array("escape"=>false)); ?>
                        <div class="card_info">
                            <div class="title" style="height:auto">
                                <?php echo $html->link($gal_provider["User"]["name"],array("controller"=>"gallery","action"=>"photographer",$gal_provider["GalProvider"]["id"],Inflector::slug($gal_provider["User"]["name"],"-")),array("class"=>"single-line")); ?>
                            </div>
                        </div>
                    </li>
                    <?php
                }
                ?>
            </ul>
        </div>

    </div>
</div>

<script>




function render_search_result(resp){

    var html = "";
    html = html + '<li class="card boxcard">';

                        <img src="https://s3.amazonaws.com/myshaadi/backup/images/gallery/cover/2df5cc3e21abddf9e5f42ec13f130082.jpg" class="cover_image" alt="">                       <div class="card_info">
                            <div class="title" style="height:auto">
                                <a href="/agm2/main/gallery/photographer/121/84mmStudio" class="single-line">84mmStudio</a>                         </div>
                        </div>
                    </li>
}

</script>

【问题讨论】:

  • 您已将 get_providers 作为控制器名称并在您的示例中提供了“gal_providers”。
  • 对不起..应该是gal_providers
  • 粘贴你的ctp文件代码
  • 尝试在您的错误功能中发出警报,或者请直接将网址提供给您的浏览器并检查它是否返回任何响应
  • @Dilip 请查看我更新的问题

标签: jquery ajax controller cakephp-1.3


【解决方案1】:

在你的函数 api_search() 中使用 echo 而不是 return 并尝试。

api_search() {
echo 'Hello';
}

【讨论】:

    【解决方案2】:
    function api_search() {
    echo "Hello";
    exit;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多