可以't调用函数php,ajax
Can't call function php, ajax
我使用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中执行它。
实际应该做的是echo
或return
,该变量并将其分配给对象。
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.php:020.0120262616谈话::hej()。。''comment_intert.php:20
(!)警告:log()要求参数1为双精度,字符串在C:''wamp''www''ajax''comments_pasta.php中的7行调用堆栈#TimeMemoryFunctionLocation10.0000247944{main}()。。''comment_intert.php:020.0120262616谈话::hej()。。''comment_intert.php:2030.0210263032http://www.php.net/function.log'target='_new'>日志()。。''comments_pasta.php:7{"userId":1,"comment":"test commenttext","userName":"alex"}
如果我删除函数调用,我会在控制台中得到我想要的东西。log:
asdasd Alexander Lundh 1 comment_insert.js:18响应text={"userId":1,"comment":"asdasd","userName":"alex"}
- 通过Ajax将JavaScript函数传递给PHP文件
- 在javascript函数中使用php变量
- 调用php数组中的JS函数
- 执行php函数的onclick事件的其他替代方案
- 使用ajax用jquery调用php函数
- 如何通过ajax增加/减少PHP变量值并重新加载函数
- php函数的ajax html$_POST值返回null
- 如何检查这个代码点火器php函数是否工作,该函数是否在ajax url中使用
- 无法识别从php创建的Js函数
- PHP/JS-EchoJs函数PHP curl发布后的通知
- 如果函数PHP中的条件为true,则禁用链接
- Jquery在Wordpress中不起作用;函数php
- Ajax 中的函数 php
- 函数.php当变量未硬编码时脚本中断
- 可以't调用函数php,ajax
- 获取我的数据库值作为字符串传递给我的javascript函数PHP
- 从下拉菜单中发送和设置3个选定的值到JavaScript函数PHP
- 通过触发AJAX函数——PHP
- 如何从ajax函数(PHP)为js变量赋值
- 通过参数将php变量传递给javascript触发器函数PHP,JAVASCRIPT