【发布时间】:2015-08-24 19:40:34
【问题描述】:
我有一个简单的脚本,我想在其中使用 AJAX 将数据发布到服务器,然后将其吐出(回显),但它不起作用...
这是我的输出:
Your viewport width is 1338x684
Ajax wasn't initialized!
并且在这种情况下,宽度和高度仅在 PHP 脚本没有获取任何数据时设置为 880px * 495px。
testajax.php的网页头:
<head>
<script type='text/javascript' src='js/jquery-1.7.2.min.js'></script> <!-- Main Java library script-->
<!-- The following 3 scripts are utilized by the Accordion Menu in the aside side bar -->
<script type='text/javascript' src='js/jquery.cookie.js'></script> <!-- Acco. Menu script-->
<script type='text/javascript' src='js/jquery.hoverIntent.minified.js'></script><!-- Acco. Menu script-->
<script type='text/javascript' src='js/jquery.dcjqaccordion.2.7.min.js'></script> <!-- Acco. Menu script-->
<!--The following 2 scripts are utilized by the Pan and Zoom Slide Show -->
<script type="text/javascript" src="js/jquery.carouFredSel-6.2.1.js"></script>
<script type="text/javascript" src="js/panzoomslideshow.js"></script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Testing Ajax</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.min.css"> <!-- used to normalize-->
<link rel="stylesheet" href="css/main.css"> <!-- Main CSS styling-->
<script type="text/javascript">
document.write('<p>Your viewport width is '+ window.innerWidth+'x'+window.innerHeight+'</p>');
$(document).ready(function() {
$.ajax({
url: "ajaxresponse.php",
type: "POST",
dataType: "json",
data: {
width : window.innerWidth,
height : window.innerHeight
},
success: function( data, textStatus, jQxhr ){
$("#response pre").html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
});
</script>
<!-- ********** panzooom.php prints the java script to make a pan and zoom transition slide show, depending on the width and heigth of the viewport *********** -->
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>window.html5 || document.write('<script src="js/vendor/html5shiv.js"><\/script>')</script>
<!--<![endif]-->
<?php include 'ajaxresponse.php';?>
</head>
这里是 ajaxresponse.php 文件:
<?php
if (is_ajax()) {
if (isset($_POST["data"]) && !empty($_POST["data"])) { //Checks if data value exists
$data = $_POST["data"];
echo "Ajax was initialized :)<br>";
echo '<p>var_dump($_POST):<br>';
var_dump( $_POST );
}else{
"Ajax was initialized, but the value isn't set or it's empty (same same)!<br>";
}
}else{
echo "Ajax wasn't initialized!<br>";
}
//Function to check if the request is an AJAX request
function is_ajax() {
echo "\$_SERVER['HTTP_X_REQUESTED_WITH'] -> " . $_SERVER['HTTP_X_REQUESTED_WITH'] . "<br>";
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
?>
【问题讨论】:
-
您是否查看过浏览器的开发者工具(-> 网络资源)并实际看到 AJAX 请求已关闭?还有
I want to post data using AJAX to the server and then spit it out (echo), but it won't work...到底是什么意思?我的意思是it won't work不是错误描述。就好像患者会去看医生并说I'm hurt。 -
问题是,你为什么要关闭处理并将contentType设置为JSON,然后你试图将数据作为
$_POST['width']获取,就好像它是www-encoded一样't, 现在是 JSON 了? -
虽然可能不会导致问题,但您应该将 PHP 的输出从
<head>元素移动到<body>元素中。 -
获得了所有三个 cmets,并将尝试移动代码并研究如何将其从 JSON 更改为简单的字符串文本。在开发人员工具下,我得到了控制台和网络,我也会尝试为您提供这些信息。
-
adeneo - 我在我的 Comodo 浏览器(基本上是 Chrome)中截取了网络部分的屏幕截图,在 XHR 下有一个各种文件的列表,主要是带有状态、类型、启动器、大小和时间的 .js,连同时间线。在这个列表中,我找到了 testajax.php,它的大小是 7.0KB,时间是 1.65 秒,这难道不是表明某些东西被触发了吗?