使用 AJAX 从 PHP 文件获取响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14918462/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get response from PHP file using AJAX
提问by user115422
So here's my issue, I am using AJAX (jQuery) to post a form to process.phpbut the page actually needs to echo out a response such as appleor plum. I'm not sure how to take the response from process.phpand have it stored as a variable...
所以这是我的问题,我使用 AJAX (jQuery) 将表单发布到,process.php但页面实际上需要回显诸如appleor 之类的响应plum。我不确定如何获取响应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.phpin json? or will plain text be fine?
我还需要process.php在 json 中回显响应吗?或者纯文本可以吗?
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请求如何命名?
回答by Marc B
<?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
}
回答by Alex
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));
    }
?>
回答by Ethan
<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>
回答by Tucker
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)
e.g.
例如
<?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...
                }
            });
回答by Pankaj Thorat
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)
        }
   });

