简体   繁体   English

使用 AJAX 从 PHP 文件获取响应

[英]Get response from PHP file using AJAX

So here's my issue, I am using AJAX (jQuery) to post a form to process.php but the page actually needs to echo out a response such as apple or plum .所以这是我的问题,我使用 AJAX (jQuery) 将表单发布到process.php但页面实际上需要回显响应,例如appleplum I'm not sure how to take the response from process.php and have it stored as a variable...我不确定如何从process.php获取响应并将其存储为变量...

Here's the code I have so far:这是我到目前为止的代码:

<script type="text/javascript">
        function returnwasset(){
            alert('return sent');
            $.ajax({
                type: "POST",
                url: "process.php",
                data: somedata;
                success function(){
                    //echo what the server sent back...
                }
            });
        }
    </script>

Also will I need to echo the response in process.php in json?我还需要在 json 中的process.php中回显响应吗? or will plain text be fine?或者纯文本可以吗?

Sorry if this sounds like a stupid question, this is my first time doing something as such in Ajax.对不起,如果这听起来像一个愚蠢的问题,这是我第一次在 Ajax 中做这样的事情。

PS: How do I name the POST request in the above code? PS:上面代码中的POST请求如何命名?

<?php echo 'apple'; ?> <?php echo 'apple'; ?> is pretty much literally all you need on the server. <?php echo 'apple'; ?>几乎是您在服务器上所需的全部内容。

as for the JS side, the output of the server-side script is passed as a parameter to the success handler function, so you'd have至于 JS 端,服务器端脚本的输出作为参数传递给成功处理函数,所以你有

success: function(data) {
   alert(data); // apple
}

The good practice is to use like this:好的做法是这样使用:

$.ajax({
    type: "POST",
    url: "/ajax/request.html",
    data: {action: 'test'},
    dataType:'JSON', 
    success: function(response){
        console.log(response.blablabla);
        // put on console what server sent back...
    }
});

and the php part is:和 php 部分是:

<?php
    if(isset($_POST['action']) && !empty($_POST['action'])) {
        echo json_encode(array("blablabla"=>$variable));
    }
?>
<script type="text/javascript">
        function returnwasset(){
            alert('return sent');
            $.ajax({
                type: "POST",
                url: "process.php",
                data: somedata;
                dataType:'text'; //or HTML, JSON, etc.
                success: function(response){
                    alert(response);
                    //echo what the server sent back...
                }
            });
        }
    </script>

in your PHP file, when you echo your data use json_encode ( http://php.net/manual/en/function.json-encode.php )在您的 PHP 文件中,当您回显数据时,请使用 json_encode ( http://php.net/manual/en/function.json-encode.php )

eg例如

<?php
//plum or data...
$output = array("data","plum");

echo json_encode($output);

?>

in your javascript code, when your ajax completes the json encoded response data can be turned into an js array like this:在您的 javascript 代码中,当您的 ajax 完成时,json 编码的响应数据可以变成这样的 js 数组:

 $.ajax({
                type: "POST",
                url: "process.php",
                data: somedata;
                success function(json_data){
                    var data_array = $.parseJSON(json_data);

                    //access your data like this:
                    var plum_or_whatever = data_array['output'];.
                    //continue from here...
                }
            });
var data="your data";//ex data="id="+id;
      $.ajax({
       method : "POST",
       url : "file name",  //url: "demo.php"
       data : "data",
       success : function(result){
               //set result to div or target 
              //ex $("#divid).html(result)
        }
   });

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

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