可以't调用函数php,ajax

Can't call function php, ajax

本文关键字:函数 php ajax 调用 可以      更新时间:2023-09-26

我使用Ajax访问我的comment_insert.php。在我的comment-insert.php中,我想调用位于另一个php文件(comments_pasta.php)中的公共静态函数。如果我调用函数,我的程序会得到一个

"未捕获的语法错误:意外的标记<"

如果我删除了这个电话,一切都会好起来的。我是ajax的新手。

function comment_post_btn_click(){
    var _comment = $('#comment-post-text').val();
    var _userId = $('#userId').val();
    var _userName = $('#userName').val();
    if(_comment.length > 0 && _userId != null){
        console.log(_comment + " " + _userName + " " +_userId);
        $.post("/ajax/comment_insert.php",
            {
                //we use this in the comment_insert.php(AJAX)
                task : "comment_insert",
                userId : _userId,
                comment : _comment
            }   
        ).success(
            function(data){
                //going to turn the Json from comment_insert.php(AJAX)into a javascript object
                comment_insert(jQuery.parseJSON(data));
                console.log("Response text = " + data);

<?php 
    session_start();
    if(isset($_POST['task']) && $_POST['task'] == 'comment_insert' ){
        require_once $_SERVER['DOCUMENT_ROOT'] . 'defines.php';
        $userId = (int)$_POST['userId'];
        $comment = addslashes(str_replace ("'n" , "<br>" , $_POST['comment']));
        $std = new stdClass();
        $std -> userId = $userId;
        $std -> comment = $comment;
        $std -> userName = $_SESSION['userName'];
        require_once ('comments_pasta.php');

        Talk:hej();

        echo json_encode($std);
    }
?>
<?php
class Talk{
    public static function hej(){
        console.log("HEJ");
    }
}
?>

实际情况是,意外的语法错误实际上是因为PHP抛出了一个错误,并且它的格式化错误报告包含类似<p> There was an error! </p>的内容,并且它在<上中断,这也巧合地使您对脚本的JSON响应无效。console.log()是一个javascript函数。你不能在PHP中执行它。

实际应该做的是echoreturn,该变量并将其分配给对象。

public static function hej(){
   return "HEJ":
}

然后在您的PHP脚本中。

$std -> userName = $_SESSION['userName'];
require_once ('comments_pasta.php');
$std->HEJ = Talk:hej();

现在,您可以在$.post()包装器的success函数中执行console.log(data.HEJ)

此外,您正在执行comment_insert(jQuery.parseJSON(data));,但是,您可以通过将'json'添加为调用的最终参数,让jQuery自动解析$.post()包装器中返回的数据,请注意:

$.post({ //object
}, 'json'); // <--final line closing $.post() wrapper

现在它只是:

comment_insert(data);

这应该可以解决问题。

尝试此ajax调用

function comment_post_btn_click()
    {

var _comment  = $('#comment-post-text').val();
var _userId   = $('#userId').val();
var _userName = $('#userName').val();

        var form_data   =   new FormData();                  
        form_data.append('_comment',_comment);
        form_data.append('_userId',_userId);
        form_data.append('_userName',_userName);

        $.ajax({
                // url          :   'userListAjax.php?r='+Math.random();,
                url         :   'userListAjax.php?r=',
                dataType    :   'text',
                cache       :   false,
                contentType :   false,
                processData :   false,
                data        :   form_data,                       
                // data: {params:[page,display]},                         
                type        :   'post',
                success     :   function(data){
                    // alert(data);
                    document.getElementById("dynamicContent").innerHTML=data;
            }
        });
    }

好吧,什么都不管用。。从comment_insert(jQuery.parseJSON(data))获取输出;我只需要删除函数的调用。如果我做console.log("Response text="+data);在用函数调用comment_insert之前,我得到了这个:

响应文本=
(!)注意:在7行C:''wamp''www''ajax''comments_pasta.php中使用未定义的常量console-假定为console调用堆栈#TimeMemoryFunctionLocation10.0000247944{main}()。。''comment_intert.php020.0120262616谈话::hej()。。''comment_intert.php20
(!)警告:log()要求参数1为双精度,字符串在C:''wamp''www''ajax''comments_pasta.php中的7行调用堆栈#TimeMemoryFunctionLocation10.0000247944{main}()。。''comment_intert.php020.0120262616谈话::hej()。。''comment_intert.php2030.0210263032http://www.php.net/function.log'target='_new'>日志()。。''comments_pasta.php7{"userId":1,"comment":"test commenttext","userName":"alex"}

如果我删除函数调用,我会在控制台中得到我想要的东西。log:

asdasd Alexander Lundh 1 comment_insert.js:18响应text={"userId":1,"comment":"asdasd","userName":"alex"}