2011-08-18 37 views
2

我想從$.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 

可以呈現將是任何幫助深深讚賞 感謝

+1

(看到你的另一個問題要求回答這個問題)如果你發佈一個很長的問題,我們很難閱讀和理解,並回答。儘量簡潔。 – Nivas

回答

0
$.post("simpleData.php", { 
      data: {chess:datas}, 
      dataType: "html", 
      async:false, 
      success: function(html){ 
       alert(html); 
       var resultAsString = html; 
       document.write(resultAsString); 
      }  
}); 

你的語法有點怪,嘗試上述版本(不過說實話,文件撰寫不應該被正常使用)。

0

我認爲你的需要是在原始文件中,而不是.php文件。

{success: function(msg){ 
       alert(msg.d); 
       var resultAsString = msg.d; 
       //document.write(resultAsString); 
       $("#output").html(resultAsString); //this will add it to the div 
    } 
2

這可能不是完整的理由,但是,當jQuery的請求數據類型:「JSON」它的方式手動顯示爲小寫,沒有大寫(不知道它的問題) - 的PHP需要識別輸出與頭JSON(也返回OK狀態),像這樣:

header("HTTP/1.1 200 OK"); 
header('Content-type: application/json'); 

您迴應任何數據之前。

也不要返回html標籤,只需調用json頭,echo輸出,然後退出()。

哦,陣列需要被編碼到JSON:

echo json_encode($_REQUEST['chess']); 

EDITS: 我想你可能已經把一些這方面的建議在錯誤的地方 - 試試這個。 你simpleData.php從開始到結束,應該是這樣的返回JSON數據:

<?php 
header("HTTP/1.1 200 OK"); 
header('Content-type: application/json'); 
echo json_encode($_REQUEST); 
exit(); 
?> 

注意缺少周圍的PHP標記外的HTML標記或任何東西的。你不能同時擁有它,一個返回HTML進行調試的腳本和一個返回適當的JSON的腳本,它不會破壞jQuery的發佈。

如果您想在查看製作帖子的HTML頁面時查看simpleData.php的JSON響應,請安裝類似firefox的firebug插件之類的工具,並觀察NET面板以便能夠準確檢查發生的事情在帖子上。

現在,從您的JavaScript刪除頁眉內容類型的東西,是在錯誤的地方加入:

$.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); 
     } 

     }); 

應該變成:

$.post("simpleData.php", { 
     data: {"chess":datas}, 
     dataType: "json", 
     async:false, 
     success: function(html){ 
      newProcessData(html); 
     } 

     }); 
2

您可以參考官方的jQuery文檔發佈到php和更新div的很好的簡單例子:

http://api.jquery.com/jQuery.post/

如:

$.post('ajax/test.html', function(data) { 
    $('.result').html(data); 
}); 
0

我不是一個PHP程序員,但根據您所提供


["data"]=> array(1) { ["chess"]=> string(2) "e8" } 

它看起來像「一盤棋」的下面的輸出數據仍然在「數據」中$ _REQUEST。你應該試着做$ _REQUEST ['data'] ['chess']來看看它是否可行。

+0

仍然不起作用。查看我的帖子的編輯,顯示最近的代碼。 – Woffy613

+0

你試過$ move_from = $ _REQUEST ['data'] ['chess'];我沒有看到每個最新的編輯 – momo