【问题标题】:Jquery Ajax Post To Cakephp ControllerJquery Ajax 发布到 Cakephp 控制器
【发布时间】:2015-11-18 14:17:19
【问题描述】:

我正在开发使用 CakePHP 作为后端和 JQuery 作为前端的 Web 应用程序。为了与服务器通信,我已经创建了表单和 jquery ajax 以及显示 ajax 向网络中的 url 发送请求值的浏览器。这是我现在拥有的:

问题

I want to display ajax post value into add.ctp which is not showing 

第 1 页 Ajax 的工作原理

<script type="text/x-javascript">   
$(".hover-star").click(function () {
$("#hover-test").css("color", "#ff6a00");

    var id= $('input[name=starrating]:checked').attr('id');

    $.ajax({
        type: "POST",
        url: '<?php echo Router::url(array('controller'=>'Reviews','action'=>'add'));?>',
        cache: false,
        data: "id="+id,
        dataType: 'HTML',
        success: function(result) {
            window.open('/app/webroot/reviews/add/'+id,'_self');

        },      
        error: function (response, desc, exception) {
            // custom error
        }
    });
});   
</script>

<form method="post">
    <input class="hover-star" type="radio" name="starrating" id="1" value="1" title=""/>
    <input class="hover-star" type="radio" name="starrating" id="2" value="3" title=""/>    
    <input class="hover-star" type="radio" name="starrating" id="3" value="3" title=""/>    
</form>

第 2 页,我想在其中显示 ajax 请求值 Add.ctp

<?php echo $this->ratingPost;?>

评论控制器添加功能

function add()
{    
    $this->ratingPost= $data = $this->data;
}

【问题讨论】:

  • @AbrarKhan 没有错误,但 chrome 浏览器网络显示 ajax 发送了值,而在单击单选按钮后,add.ctp 文件中没有显示值

标签: php jquery ajax cakephp


【解决方案1】:

看起来可能出现了一些问题。首先,您需要了解 AJAX 会将其数据 POST 到指定的 URL 并等待来自服务器的响应。响应将包含在“结果”中。

在这种情况下,您似乎正在向 /reviews/add 发布内容。当您这样做时,您的 add 方法将接收发布数据并尝试呈现 add.ctp 视图文件。这整个渲染是返回给您的 AJAX 成功“结果”的内容。在您的 add 方法中,您没有为视图设置任何内容,也没有返回任何值,因此您的“结果”为空。

在您的 AJAX 成功中,您尝试打开一个再次调用 add 方法的新窗口,并且您尝试包含参数“id”,它不会对返回的结果做任何事情,只尝试运行再次使用给定参数的方法,但您的方法无论如何都不要求任何参数...

如果您希望通过单击打开一个带有 id 值的新窗口,为什么还要使用 AJAX,只需将 id 作为参数发送到您的 add 方法。一般来说,我认为 AJAX 用于动态更新当前加载页面上的内容。我将在您的代码中突出显示我在下面谈论的内容。

你的脚本

<script type="text/x-javascript">   
  $(".hover-star").click(function () {
    $("#hover-test").css("color", "#ff6a00");
    var id= $('input[name=starrating]:checked').attr('id');

    window.open('/app/webroot/reviews/add/'+id, '_self');
  }
</script>

您的添加方法

public function add($myID=null){
  $this->set('myID', $myID);
}

如果您致力于 AJAX,您可以通过创建一个新方法来处理返回的 AJAX 的呈现来做到这一点。我只是不明白重点。您还必须将从 AJAX 方法返回的内容限制为 URL 安全编码。

AJAX方式

$.ajax({
    type: "POST",
    url: '<?php echo Router::url(array('controller'=>'Reviews','action'=>'add'));?>',
    cache: false,
    data: "id="+id,
    dataType: 'HTML',
    success: function(result) {
        window.open('** path to new method for rendering view **'+result, '_self');
    },      
    error: function (response, desc, exception) {
        // custom error
    }
});

【讨论】:

    【解决方案2】:

    在 ajax 工作的页面上

    <div id="show"></div>
    

    ajax 成功会是什么样子

    success: function (result) {
                    $("#show").html(result);
                    $("#show").show();
                }
    

    【讨论】:

    • 值显示在&lt;div id="show"&gt;&lt;/div&gt; 中,但为什么值没有显示在add.ctp 中而不是在controller.php 文件中打印或回显
    • cakephp 版本为 2.0.13
    • 在控制器中获取 cakePHP $data=$this->request->data['name'];
    • 如何返回添加ctp并显示post值?我实际上是 cakephp 的新手
    • 返回视图 $this->set('data', $data);
    猜你喜欢
    • 2015-01-28
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2013-08-27
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多