从Javascript调用php代码-AJAX使用整个页面的html进行响应
我对php还是相当陌生的。我已经读到,如果您想从按钮调用php方法,首先需要分配按钮的onclick事件来调用javascript函数,而javascript函数又必须通过ajax发布到php页面 目前,我的javascript如下:从Javascript调用php代码-AJAX使用整个页面的html进行响应,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我对php还是相当陌生的。我已经读到,如果您想从按钮调用php方法,首先需要分配按钮的onclick事件来调用javascript函数,而javascript函数又必须通过ajax发布到php页面 目前,我的javascript如下: function handle_qa_submit_comment(postId, parentPostId, elem){ //console.log(elem); var ajaxUrl = './index.php'; var el
function handle_qa_submit_comment(postId, parentPostId, elem){
//console.log(elem);
var ajaxUrl = './index.php';
var elem = encodeURIComponent(elem);
var data = {
action : 'add_comment',
data :
{
'postId' : postId,
'parentPostId' : parentPostId,
'elem' : elem
}
};
$.ajax({
url: ajaxUrl,
data: JSON.stringify(data),
contentType: 'application/json',
async: true,
}).always(function(jqxhr,textStatus){
console.log("jqxhr:");
console.log(jqxhr);
console.log("status:");
console.log(textStatus);
});
return false;
}
是的,我怀疑elem参数是错误的-为该参数提供的参数是按钮的onclick处理程序上的
在这段代码中,数据变量(json)被发布到我的php页面-index.php。我的php如下所示:
require 'asdf-include/app/csrf.class.php';
require 'asdf-include/app/csrf_handler.php';
session_start();
$GLOBALS['csrf'] = new csrf();
$GLOBALS['token_id'] = $csrf->get_token_id();
$GLOBALS['token_value'] = $csrf->get_token($token_id);
header('Content-Type: application/json');
$aResult = array();
$post = filter_input(INPUT_POST,'action');
if( !isset($post))
{
// Set base path here so this works with symbolic links for multiple installations
define('ASDF_BASE_DIR', dirname(empty($_SERVER['SCRIPT_FILENAME']) ? __FILE__ : $_SERVER['SCRIPT_FILENAME']).'/');
require 'asdf-include/asdf-index.php';
}
else
{
echo "'data found1234'" ; // I Never saw it even get to this point
}
第一件事第一-PHP对我来说仍然是全新的和闪亮的。我不确定我将如何检查$u POST superglobal的价值。重要的是,我从php得到的响应似乎是整个页面的解析html,而不是我的回应
也许我做得不对,但这里的目标是我想从ajax调用一个php函数。在我这里的例子中,我没有正确地转换JSON,但是撇开它不谈
如何访问post superglobal的内容
为什么我的响应是解析的html页面
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8"><![endif]-->
<!--[if IE 8]><html class="no-js lt-ie9"><![endif]-->
<!--[if gt IE 8]><html class="no-js"><![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" >
我找到了问题的根源。我们服务器上的PHP版本是5.4 这是我带来的检查和修复,其中包括使用一个不推荐使用的超级全局 首先,我将ajax url路由到一个空的php页面。我回显了一个简单的字符串——发现它正是jqxhr在控制台中打印的字符串——这意味着,我将始终在jqxhr中获得页面的html 其次,我使用了print\r$GLOBALS。$POST-it打印数组中未存储任何内容。但是,superglobal$HTTP_RAW_POST_数据包含我从ajax发送的json数据。有趣 我将json从$HTTP_RAW_POST_数据转换为要处理的对象:
$params = json_decode($HTTP_RAW_POST_DATA);
print_r($params);
$action = $params->action;
$postId = $params->data->postId;
$parentPostId = $params->data->parentPostId;
-
我的重要观察结果是,我不使用最新的php,将请求在我们的web服务器上升级,并且我的ajax内容落在一个不推荐使用的变量中
Javascript:
var ajaxUrl = '../test/empty.php';
var data = {
action : 'add_comment',
data :
{
'postId' : postId,
'parentPostId' : parentPostId,
'elem' : $(elem).html()
}
};
$.ajax({
url: ajaxUrl,
data: JSON.stringify(data),
contentType: 'application/json',
async: true,
type: 'POST'
}).always(function(jqxhr,textStatus){
console.log("jqxhr:");
console.log(jqxhr);
console.log("status:");
console.log(textStatus);
}).error(function(){
//TODO uncomment when finished
//submit_logout('/Account/Logoff');
});
PHP:
控制台中的输出:
Array
(
**[HTTP_RAW_POST_DATA] => {"action":"add_comment","data":{"postId":5,"parentPostId":6,"elem":""}}**
[_GET] => Array
(
)
[_POST] => Array
(
)
[_COOKIE] => Array
(
[SUPERSECRETSTUFF] => NOPE!
)
[_FILES] => Array
(
)
[GLOBALS] => Array
*RECURSION*
)
stdClass Object
(
[action] => add_comment
[data] => stdClass Object
(
[postId] => 5
[parentPostId] => 6
[elem] =>
)
)
empty.js:32 status:
empty.js:33 success
我忘了记。php有开始标记,但没有结束标记,以避免意外输出。我注意到在传递给.ajax的对象中没有type:post,所以您对index.php发出了GET请求。没错,忘了这一点。然而,我也尝试了$.post,这给了我同样的结果。我删除了$.post并将其替换为$.ajax-添加了类型:“post”,但结果仍然相同。
Array
(
**[HTTP_RAW_POST_DATA] => {"action":"add_comment","data":{"postId":5,"parentPostId":6,"elem":""}}**
[_GET] => Array
(
)
[_POST] => Array
(
)
[_COOKIE] => Array
(
[SUPERSECRETSTUFF] => NOPE!
)
[_FILES] => Array
(
)
[GLOBALS] => Array
*RECURSION*
)
stdClass Object
(
[action] => add_comment
[data] => stdClass Object
(
[postId] => 5
[parentPostId] => 6
[elem] =>
)
)
empty.js:32 status:
empty.js:33 success