1

我是 jquery ajax 的新手。我在 cakephp 工作,我有以下事情要做:我有 7 个链接:sun, mon, tue, wed, ...

现在单击每个链接,我希望将该链接的文本存储到一个可以在控制器中访问的数组中。我希望使用 jquery ajax 来做到这一点。

以下是我的代码不起作用(jquery ajax 测试代码):

<?php
    echo $this->Html->link('sun', 
    array('onclick' => 'callAjax();'), 
    array('class' => 'dayss'));
?>
<script>
function callAjax() {

        $.ajax({ 
            url: "rideoffers/ride_offer",
            type: "POST",
            success: function() {
            console.log("hi");
            },
            error: function() {
            console.log("error");
            }
        });
return true;
}
</script>    

上面的代码不会打印 hi 或错误。我哪里错了?我该如何解决?

编辑:迪佩什帕尔马

    <pre>CakeRequest Object
(
    [params] => Array
        (
            [plugin] => 
            [controller] => Rideoffers
            [action] => ride_array
            [named] => Array
                (
                )

            [pass] => Array
                (
                )

        )

    [data] => Array
        (
        )

    [query] => Array
        (
        )

    [url] => Rideoffers/ride_array
    [base] => /carpooling
    [webroot] => /carpooling/
    [here] => /carpooling/Rideoffers/ride_array
    [_detectors:protected] => Array
        (
            [get] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => GET
                )

            [post] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => POST
                )

            [put] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => PUT
                )

            [delete] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => DELETE
                )

            [head] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => HEAD
                )

            [options] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => OPTIONS
                )

            [ssl] => Array
                (
                    [env] => HTTPS
                    [value] => 1
                )

            [ajax] => Array
                (
                    [env] => HTTP_X_REQUESTED_WITH
                    [value] => XMLHttpRequest
                )

            [flash] => Array
                (
                    [env] => HTTP_USER_AGENT
                    [pattern] => /^(Shockwave|Adobe) Flash/
                )

            [mobile] => Array
                (
                    [env] => HTTP_USER_AGENT
                    [options] => Array
                        (
                            [0] => Android
                            [1] => AvantGo
                            [2] => BlackBerry
                            [3] => DoCoMo
                            [4] => Fennec
                            [5] => iPod
                            [6] => iPhone
                            [7] => iPad
                            [8] => J2ME
                            [9] => MIDP
                            [10] => NetFront
                            [11] => Nokia
                            [12] => Opera Mini
                            [13] => Opera Mobi
                            [14] => PalmOS
                            [15] => PalmSource
                            [16] => portalmmm
                            [17] => Plucker
                            [18] => ReqwirelessWeb
                            [19] => SonyEricsson
                            [20] => Symbian
                            [21] => UP\.Browser
                            [22] => webOS
                            [23] => Windows CE
                            [24] => Windows Phone OS
                            [25] => Xiino
                        )

                )

            [requested] => Array
                (
                    [param] => requested
                    [value] => 1
                )

        )

    [_input:protected] => 
)
</pre>
4

3 回答 3

0

添加return false;事件onclick并尝试返回 ajax 本身。

<?php
   echo $this->Html->link('sun',  
   array('class' => 'dayss'));
?>
<script>
 $(function(){
   $('.dayss').on('click', callAjax);
 });

 function callAjax() {
    return $.ajax({ 
        url: "rideoffers/ride_offer",
        type: "POST",
        success: function(data) {
        console.log("hi");
        console.log(data);
        },
        error: function() {
        console.log("error");
        }
    });
  }
</script> 
于 2013-02-12T07:06:22.860 回答
0

我不确定您的url参数是否正确:

url: "rideoffers/ride_offer"

是吗?您能否复制不使用 ajax 的请求并获得所需的结果(例如,让您的 ajax 目标 url 使用 GET 接受相同的请求并测试它的输出)?

如果没有,也许你的控制台搞砸了?尝试使用警报而不是控制台转储调试行:

例如

alert("hi")
于 2013-02-12T07:04:09.973 回答
0

这是解决方案。

代码必须如下。

echo $this->Html->link('sun', array(),array('onclick' => 'callAjax();','class' => 'dayss'));

第二个参数Html->link是 forhref并且您在其中分配onclick事件。

回应评论

<script type="text/javascript">
function callAjax()
{
    var textString = $(this).text();
    $.ajax(
    {
        url: "rideoffers/ride_offer",
        type: "POST",
        data : textString,
        success: function()
        {
            console.log("hi");
        },
        error: function()
        {
            console.log("error");
        }
    });
}
</script>

控制器

<?php

    function ride_offer()
    {
        //to test data is passed as needed.
        pr($this->params);
        exit;
    }
于 2013-02-12T07:58:25.563 回答