[英]jquery cannot post with ajax php
我有這個簡單的腳本,我想在其中使用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';
}
?>
在這里,您告訴jQuery當它發出請求時:
contentType: 'application/json', processData: false,
在這里,您不傳遞JSON :
data: { width : window.innerWidth, height : window.innerHeight },
並在PHP中:
您嘗試從$_POST
讀取,如果請求格式為JSON,則不會初始化。
isset($_POST["data"]
從JavaScript中刪除contentType和processData配置。 讓jQuery進行其默認的使用表單編碼對數據進行編碼的操作。
您正在嘗試讀取名為data
:
$data = $_POST["data"];
但您發送的唯一數據稱為width
和height
:
data: { width : window.innerWidth, height : window.innerHeight },
您需要測試實際發送的數據。
您說( dataType: 'json',
)您希望響應中testajax.php
JSON,但是testajax.php
返回HTML文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.