简体   繁体   English

Jquery ajax 无 PHP 响应

[英]Jquery ajax No PHP response

My script runs fine and it alerts me the data is saved.我的脚本运行良好,它提醒我数据已保存。 but for Debug reason I want to see the PHP ("Procees.php") response as well in html format.但出于调试原因,我想查看 PHP(“Procees.php”)响应以及 html 格式。 I can see it in firebug but I do not know how to out put it using jquery.我可以在萤火虫中看到它,但我不知道如何使用 jquery 输出它。 My codes are as follows;我的代码如下;

            var dataString ='doctype='+ doctype + '&paytype=' + paytype + '&docno=' + docno + '&bookno=' + bookno + '&prname=' + prname +'&pename='+pename+'&paydate='+paydate+'&inpdate='+indate 
        +'&e1='+e1 +'&e2='+e2 +'&e3='+e3 +'&e4='+e4 +'&e5='+e5 +'&e6='+e6 +'&e7='+e7 +'&e8='+e8 
        +'&accname1='+accname1 +'&accname2='+accname2 +'&accname3='+accname3 +'&accname4='+accname4 +'&accname5='+accname5 +'&accname6='+accname6 +'&accname7='+accname7 +'&accname8='+accname8 
        +'&dr1='+dr1 +'&dr2='+dr2 +'&dr3='+dr3 +'&dr4='+dr4 +'&dr5='+dr5 +'&dr6='+dr6 +'&dr7='+dr7 +'&dr8='+dr8
        +'&cr1='+cr1 +'&cr2='+cr2 +'&cr3='+cr3+'&cr4='+cr4 +'&cr5='+cr5 +'&cr6='+cr6 +'&cr7='+cr7 +'&cr8='+cr8 +'&ref='+ref;


        //alert (dataString);return false;
        $.ajax({
        type: "POST",
        url: "bin/process.php",
        data: dataString,
        cache:false,
        datatype: 'json',               
        success: function(data) {

        $('#display').html("<h2>Data submitted!</h2>")
        .append("<p>Wait........</p>")
        .hide()
        .fadeIn(1500, function() {
        $('#display').append("<img id='checkmark' src='images/check-black.jpg' />");
        $('#display').html("<h3>Being Processed...</h3>")
        .fadeIn(1500, function() {
        $('#display').append("Saved <img id='checkmark' src='images/check-black.jpg' />")
        $('#thisfrm').get(0).reset();
        $("#indate").val(indate)






        });

        });
        }
        });


        return false;   

My PHP echos我的 PHP 回声

                echo 'This is processing file.\n';


            $doctype = $_REQUEST['doctype'];
            $paytype = $_REQUEST['paytype'];
            $docno = $_REQUEST['docno'];
            $bookno = $_REQUEST['bookno'];
            $prname = $_REQUEST['prname'];
            $pename = $_REQUEST['pename'];
            $paydate = $_REQUEST['paydate'];
            $inpdate = $_REQUEST['inpdate'];
            $ref = $_REQUEST['ref'];


            echo $doctype."<br>";
            echo $paytype."<br>";
            ....
            ... and so on

but it is not shown anywahere on the webpage.但它没有显示在网页上的任何地方。

Thanks in advance for any help.提前感谢您的帮助。

You need to json_encode your response since your jQuery is expecting json!您需要 json_encode 您的响应,因为您的 jQuery 期待 json!

echo json_encode(array("doctype"=>$doctype, "paytype"=>$paytype));

The data that is output by PHP during an AJAX request will be in the data variable of your success callback function.在 AJAX 请求期间,PHP 的 output 数据将在您的success回调 ZC1C425268E68A45 的data变量中。 If you want to append the data onto your page, you'll need to do so in your success function.如果您想将 append 的数据放到您的页面上,您需要在您的成功 function 中这样做。

$.ajax({
    type: "POST",
    url: "bin/process.php",
    data: dataString,
    cache:false,          
    success: function(data) {
        // the data variable contains all PHP output during this request
        $('#display').html(data);
    }
});

Also note, your script currently specified that it is a JSON request - that jQuery ought to treat the output as JSON data.另请注意,您的脚本当前指定它是 JSON 请求 - jQuery 应该将 output 视为 Z0ECD18ZC1D7A2D7 数据。 However, you are outputting text/html data in your PHP snippet.但是,您在 PHP 代码段中输出文本/html 数据。 Either a) append your html into one variable, then call print json_encode($mydata); a) append 你的 html 变成一个变量,然后调用print json_encode($mydata); , OR b) remove the datatype property from the request. , 或 b) 从请求中删除datatype属性。 jQuery will "auto-detect" the response type. jQuery 将“自动检测”响应类型。 The above example is assuming you do NOT change the PHP code, below is details on how to correctly use JSON instead (change to PHP required).上面的例子假设你不改变 PHP 代码,下面是关于如何正确使用 JSON 的详细信息(需要更改为 PHP)。

To do the JSON method, instead of using echo , use a variable:要执行 JSON 方法,而不是使用echo ,请使用变量:

$html = '';
$html .= 'Doctype: '.$doctype;
$html .= 'Something elese: '.$something_else;
// etc...
die(json_encode(array('html'=>$html)));

Then your jQuery ajax function looks like this:然后你的 jQuery ajax function 看起来像这样:

$.ajax({
    type: "POST",
    url: "bin/process.php",
    data: dataString,
    cache:false,
    datatype: 'json',           
    success: function(data) {
        $('#display').html(data.html);
    }
});

Put this inside your success function.把它放在你的成功 function 里面。

$('#display').append(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM