Domanda

I am currently running CakePHP 2.3.4 on my localhost (xampp 1.8.1) with VirtualHosts (http://sub.local/ points to c:/xampp/htdocs/cake/app/webroot/).

I want to build a comment function for my news page using AJAX to add the comments. Thus i only got a textarea as form field to enter the comment:

<textarea class="expanding" placeholder="Write your comment..." name="comment[<?php echo $news['News']['id']; ?>]"></textarea>

Nothing more here but the news text itself and the rest of the page. The text will be submitted on pressing enter, what is working so far. The full js function:

$this->Js->buffer('
            $("textarea").keypress(function(e) {
                if(e.which == 13) {
                    if ($(this).val() != "") {
                        // submit via ajax
                        var formData = $(this).serialize();

                        ' . $this->Js->request(
                                array(
                                    'controller' => 'comments',
                                    'action' => 'add',
                                    '1'
                                ),
                                array(
                                    'method' => 'POST',
                                    'async' => true,
                                    'type' => 'json',
                                    'data' => 'Testing comment.',
                                    'success' => '
                                                // delete textarea value
                                                $(this).val("");

                                                // add to form
                                                $("#c-comments").prepend("comment div following here");
                                            ',
                                    'error' => '
                                                $("#c-errorBoxes").html(
                                                \'    Your comment was not added. \' + textStatus + \' \' + errorThrown + \' </div>\'
                                                );
                                            '
                                )
                            )
                        . '
                    }

                    e.preventDefault();
                }
            });
');

Both codes are what i wrote in the view file. The comments/add/ action is the following:

public function add() {
        $id = 1;
        $this->Comment->recursive = -1;
        $comment = $this->Comment->findById($id);
        return json_encode($comment);
    }

This is just for testing purposes and should work. But nevertheless i get an 500 Internal Server Error in Firebug and also the error box is shown on the page. By now i don't know what to do else. I tried a lot of things i read here, but none were helpful.

The complete database adding function is not shown here because even the success function of ajax being called is not working with this code. The output of the JavaScript in the HTML is this (bit more code that i deleted on top):

<script type="text/javascript">
//<![CDATA[
$(document).ready(function () {
            $("textarea").autoResize();
            $("a.comments").click(function() {
                $(this).parent().parent().next("div.c-contentBodyBoxHide").slideToggle();
            });
            $("textarea").keypress(function(e) {
                if(e.which == 13) {
                    if ($(this).val() != "") {
                        // submit via ajax
                        var formData = $(this).serialize();

                        $.ajax({async:true, data:"Testing comment.", dataType:"json", error:function (XMLHttpRequest, textStatus, errorThrown) {
                                                $("#c-errorBoxes").html(
                                                '<div class="c-box c-error">' +
                                                '    <img src="/img/status/error_32.png" alt="" />' +
                                                '    Your comment was not added. ' + textStatus + ' ' + errorThrown + ' </div>'
                                                );
                                            }, success:function (data, textStatus) {
                                                // delete textarea value
                                                $(this).val("");

                                                // hide no comments if shown
                                                $("#c-commentsNone").hide();

                                                // add to form
                                                $("#c-comments").prepend("bla");

                                                // remove possible error message
                                                $("#c-errorBoxes").html("");
                                            }, type:"POST", url:"\/comments\/add\/1"});
                    }

                    e.preventDefault();
                }
            });
        });
//]]>
</script>

I am thankful for some more ideas.

È stato utile?

Soluzione

Solved the problem as the hint with /app/tmp/logs gave me the error message. The problem was, that the add action in the controller couldn't find the view add.ctp (because i created none).

Do stop the controller from rendering a view, just add

$this->autoRender = false;

in the method (i chose the beginning) and no view is searched. The request is working now!

Thanks for your help!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top