我想從$.post
返回值,並且我無處可去。這裏是代碼片段:如何從PHP返回來自AJAX文章的數據?
<!-- language: lang-js -->
$(document).ready(function(){
$('.d1').dblclick(function() {
$(this).css("background-color","blue");
var datas = $(this).attr('id');
$.post(
"simpleData.php",
{"chess":datas},
{dataType: "JSON"},
{async:false},
{contentType: "application/json; charset=utf-8"},
{success: function(msg){
alert(msg.d);
var resultAsString = msg.d;
document.write(resultAsString);
}
});
});
});
這裏是輸出的部門。我這裏沒有收到輸出:
<!-- language: lang-php -->
<div id = "output">
<?php
echo "This is the default output <br />";
echo $_REQUEST['chess'];
?>
<?php
print_r($_REQUEST);
?>
這裏是被稱爲PHP程序(simpleData.php
):我收到這個輸出正確的
<!-- language: lang-php -->
<html>
<body
<?php
$move_from = $_REQUEST['chess'];
var_dump($_REQUEST);
echo "this is move_from $move_from";
?>
</body>
</html>
,但它永遠不會出現在div。另外,我沒有收到來自警報的輸出。
請幫忙和建議。
這裏是更新代碼,遺憾的是仍然無法正常工作。結果出現在div中,但它所說的是「未定義」。此外,頁面上的棋盤被清除。
<script type="text/javascript">
$.ajaxSetup ({
cache: false
});
$(document).ready(function(){
$('.d1').dblclick(function() {
$(this).css("background-color","blue");
var datas = $(this).attr('id');
$.post("simpleData.php", {
data: {"chess":datas},
dataType: "json",
async:false,
contentType: "application/json; charset=utf-8",
header: "HTTP/1.1 200 OK",
success: function(html){
var resultAsString = html;
$("#output").html(resultAsString);
document.write(resultAsString);
}
});
</script>
這裏是程序[simpleData.php]被稱爲:
<html>
<body
<?php
$move_from = $_REQUEST['chess'];
var_dump($_REQUEST);
echo "this is move_from $move_from";
?>
</body>
</html>
下面是從螢火蟲的HTML輸出:
Notice: Undefined index: chess in /var/www/simpleData.php on line 12 array(6) { ["data"]=> array(1) { ["chess"]=> string(2) "e8" } ["dataType"]=> string(4) "json" ["async"]=> string(5) "false" ["contentType"]=> string(31) "application/json; charset=utf-8" ["header"]=> string(15) "HTTP/1.1 200 OK" ["success"]=> string(9) "undefined" } this is move_from
幫助和建議,請。 感謝
這是最近代碼:
<script type="text/javascript">
$.ajaxSetup ({
cache: false
});
$(document).ready(function(){
$('.d1').dblclick(function() {
$(this).css("background-color","blue");
var datas = $(this).attr('id');
$.post("simpleData.php", {
data: {"chess":datas},
dataType: "json",
async:false,
contentType: "application/json; charset=utf-8",
header: "HTTP/1.1 200 OK",
success: function(html){
newProcessData(html);
}
});
});
});
function newProcessData(html){
var resultAsString = html;
$("#output").html(resultAsString);
document.write(resultAsString);
alert("yippee");
}
這裏是(simpleData.php)被調用的程序[它沒有改變]
<html>
<body>
<?php
$move_from = $_REQUEST['chess'];
var_dump($_REQUEST);
echo "this is move_from $move_from";
?>
</body>
</html>
我知道,代碼到達函數newProcessData,因爲顯示了警報。
下面是Firebug的輸出:
頁眉:
Date Mon, 22 Aug 2011 04:01:42 GMT
Server Apache/2.2.19 (Debian)
X-Powered-By PHP/5.3.7-1
Vary Accept-Encoding
Content-Encoding gzip
Content-Length 401
Keep-Alive timeout=15, max=99
Connection Keep-Alive
Content-Type text/html
Request Headersview source
Host localhost
User-Agent Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0 Iceweasel/6.0
Accept */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://localhost/jq3_test.php
Content-Length 137
Pragma no-cache
Cache-Control no-cache
POST:
async false
contentType application/json; charset=utf-8
dataType json
data[chess] e8
header HTTP/1.1 200 OK
success undefined
Source
data%5Bchess%5D=e8&dataType=json&async=false&contentType=application%2Fjson%3B+charset%3Dutf-8&header=HTTP%2F1.1+200+OK&success=undefined
HTML:
Notice: Undefined index: datas in /var/www/simpleData.php on line 13 array(6) { ["data"]=> array(1) { ["chess"]=> string(2) "e8" } ["dataType"]=> string(4) "json" ["async"]=> string(5) "false" ["contentType"]=> string(31) "application/json; charset=utf-8" ["header"]=> string(15) "HTTP/1.1 200 OK" ["success"]=> string(9) "undefined" } this is move_from
可以呈現將是任何幫助深深讚賞 感謝
(看到你的另一個問題要求回答這個問題)如果你發佈一個很長的問題,我們很難閱讀和理解,並回答。儘量簡潔。 – Nivas