Ajax函数没有得到cakephp caontroller函数的任何响应

时间:2013-12-04 05:28:49

标签: jquery ajax controller cakephp-1.3

我有一个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>

2 个答案:

答案 0 :(得分:0)

在函数api_search()中使用echo而不是return,然后尝试。

api_search() {
echo 'Hello';
}

答案 1 :(得分:0)

function api_search() {
echo "Hello";
exit;
}