使用AJAX从PHP文件获得响应。

时间:2022-10-08 17:34:55

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. I'm not sure how to take the response from process.php and have it stored as a variable...

这是我的问题,我正在使用AJAX (jQuery)来发布一个要处理的表单。但是页面实际上需要响应,比如apple或plum。我不知道如何从过程中得到响应。把它存储为变量…

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? or will plain text be fine?

我还需要在过程中对响应进行响应。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请求?

5 个解决方案

#1


35  

<?php echo 'apple'; ?> is pretty much literally all you need on the server.

< ?php echo '苹果';>基本上就是你在服务器上所需要的一切。

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
}

#2


23  

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

#3


15  

<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>

#4


9  

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

#5


0  

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

#1


35  

<?php echo 'apple'; ?> is pretty much literally all you need on the server.

< ?php echo '苹果';>基本上就是你在服务器上所需要的一切。

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
}

#2


23  

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

#3


15  

<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>

#4


9  

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

#5


0  

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