我对 php 还很陌生。我读过,如果你想从按钮调用 php 方法,你首先需要分配按钮的 onclick 事件来调用 javascript 函数,而该函数又必须通过以下方式发布到 php 页面 Ajax 。
目前,我的javascript如下:
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
处理程序上的 this
这段代码中发生的情况是,data
变量 - 作为 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 对我来说仍然是全新的、 Shiny 的。我不确定如何检查 $_POST
超全局的值。重要的是,我从 php 得到的响应似乎是整个页面解析的 html,而不是我回显的内容。
也许我的处理方式是错误的 - 但这里的目标是我想从 ajax 调用 php 函数。在我的示例中,我没有正确转换 JSON - 但除此之外,
- 如何访问 super 全局帖子的内容
为什么我的响应是已解析的 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。
这是我的检查和“修复”,其中包括使用已弃用的 Super Global。
首先 - 我将 ajax url 路由到一个“空”php 页面。我回显了一个简单的字符串 - 发现它正是 jqxhr
中的字符串。在控制台中打印 - 意思是,我总是会得到 html
jqxhr
中的页面.
其次 - 我用了 print_r($GLOBALS);
。 $POST
中未存储任何内容 - 它打印 array ()
。然而,超全局$HTTP_RAW_POST_DATA
包含我从ajax发送的json数据。有趣。
我从 $HTTP_RAW_POST_DATA
转换了 json到要使用的对象:
$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:
print_r($GLOBALS);
// This has been deprecated in PHP 5.6.0 - We currently use 5.4
$params = json_decode($HTTP_RAW_POST_DATA);
print_r($params);
$action = $params->action;
$postId = $params->data->postId;
$parentPostId = $params->data->parentPostId;
$elem = $params->data->elem;
控制台输出:
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
关于javascript - 从 Javascript 调用 php 代码 - AJAX 响应整个页面 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28739387/