[英]Can't call function php, ajax
我使用Ajax來訪問我的comment_insert.php。 在我comment_insert.php我想打電話給位於另一個PHP文件(一個公共靜態函數這就是comments_pasta.php
)。 如果我調用該函數,我的程序會得到一個
“未捕獲到的SyntaxError:意外令牌<”
如果我刪除通話,一切正常。 我是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>
<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));
但是,您可以讓jQuery通過在調用的最后一個參數中添加'json'
作為自動解析$.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之前,我得到以下信息:
回復文字=
Call Stack #TimeMemoryFunctionLocation 10.0000247944{main}( )..\\comment_insert.php : 0 20.0120262616Talk::hej( )..\\comment_insert.php : 20 (!)注意:使用未定義的常量控制台-在第行的C:\\ wamp \\ www \\ ajax \\ comments_pasta.php中假定為“控制台”,調用堆棧#TimeMemoryFunctionLocation 10.0000247944 {main}().. \\ comment_insert.php : 0 20.0120262616通話:: hej().. \\ comment_insert.php : 20
Call Stack #TimeMemoryFunctionLocation 10.0000247944{main}( )..\\comment_insert.php : 0 20.0120262616Talk::hej( )..\\comment_insert.php : 20 30.0210263032http://www.php.net/function.log' target='_new'>log ( )..\\comments_pasta.php : 7 {"userId":1,"comment":"test commenttext","userName":"alex"} (!)警告:log()期望參數1為double,第行的C:\\ wamp \\ www \\ ajax \\ comments_pasta.php中給出的字符串調用堆棧#TimeMemoryFunctionLocation 10.0000247944 {main}().. \\ comment_insert.php : 0 20.0120262616Talk :: hej().. \\ comment_insert.php : 20 30.0210263032http://www.php.net/function.log'target ='_ new'> log()。\\\\ comments_pasta.php : 7 {“ userId“:1,”評論“:”測試注釋文本“,” userName“:” alex“}
如果我刪除函數調用,我會在console.log中得到我想要的:
asdasd Alexander Lundh 1 comment_insert.js:18響應文本= {“ userId”:1,“ comment”:“ asdasd”,“ userName”:“ alex”}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.