如何从PHP的AJAX后返回的数据?数据、PHP、AJAX

2023-09-10 14:27:05 作者:爷是黑户

我想返回从 $值。后,和我一事无成。下面是code片段:

I am trying to return values from $.post, and am getting nowhere. Here are the code snippets:

<!-- 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);
    }

          });       
    });

});

下面是师为输出。我没有收到输出位置:

Here is the division for the output. I receive no output here:

<!-- language: lang-php -->    

<div id = "output">
<?php
echo "This is the default output <br />";


echo $_REQUEST['chess'];

?>                      

<?php

print_r($_REQUEST);

?> 

下面是被称为PHP程序( simpleData.php ):

Here is the php program that is being called (simpleData.php):

<!-- language: lang-php -->

<html>
<body
<?php
$move_from = $_REQUEST['chess'];
var_dump($_REQUEST);
echo "this is move_from  $move_from";
?>

</body>
</html>

我收到这个正确的输出,但它永远不会出现在股利。另外,我收不到任何的报警输出。

I receive the correct output from this, but it never appears in the div. Also, I receive no output from the alert.

帮助和建议,请。

以下是更新code 其中,令人遗憾的是仍然无法正常工作。结果出现在股利,但所有它说的是不确定。此外,这是页面上的棋盘被消灭了。

Here is updated code which, regrettably still does not work. The result appears in the div, but all it says is "undefined". Additionally, the chessboard which is on the page is wiped out.

<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]被称为:

Here is the program [simpleData.php] that is called:

<html>
<body

 <?php
$move_from = $_REQUEST['chess'];
var_dump($_REQUEST);
echo "this is move_from  $move_from";

?>

</body>
</html>

下面是从萤火虫的HTML输出:

Here is the html output from the Firebug:

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

帮助和建议,请。 谢谢

Help and advice, please. Thanks

这是最近code:

<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)它具有的不可以更改]

Here is the program that is called (simpleData.php)[it has not changed]

<html>
 <body>
<?php
$move_from = $_REQUEST['chess'];
var_dump($_REQUEST);
echo "this is move_from  $move_from";
?>

</body>
</html>

我知道,code达到功能newProcessData,因为显示警报。

I know that the code reached the function newProcessData, since the alert is shown.

下面是萤火虫的输出:

标题:

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:

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:

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

任何可以被渲染的帮助会深深AP preciated 谢谢

Any help that can be rendered will be DEEPLY appreciated Thanks

推荐答案

您可以参考官方的jQuery文档发布到PHP和更新div的漂亮简单的例子:

You can refer to the official jquery documentation for nice simple examples of posting to php and updating div's:

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

例如:

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