如何在ajax调用中获取响应数据

时间:2022-10-09 00:23:36

I have a php file(index.php) with two fields when i post that form, in javascript i am doing form data serialize and send it to next php page(results.php) through ajax. When i try to print the data inside success it is not printing. FInd the below code.

当我发布该表单时,我有一个带有两个字段的php文件(index.php),在javascript中我正在进行表单数据序列化并通过ajax将其发送到下一个php页面(results.php)。当我尝试在成功内部打印数据时,它不会打印。以下代码。

<html>
<head>
    <title></title>
    <script src="../scripts/jquery-1.9.1.js"></script>
</head>
<body>
    <form method="post" name="index" id="indexform">
        <table border="1">
            <tr>
                <td>Name:</td>
                <td><input type="text" name="fname"></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" name="sendData"></td>
            </tr>
        </table>
    </form>
</body>
<script type="text/javascript">
    $( "#indexform" ).on( "submit", function( event ) {
          event.preventDefault();
          console.log( $(this).serialize() );
          var formdata = $(this).serialize();
         // alert(formdata);
          $.ajax({
                type:"POST",
                url:"result.php",
                dataType:'json',
                data:formdata,
                success: function(data){
                    alert(data);
                }
          });
        });
    </script>

In the above i cant print the data inside the success callback.

在上面我无法打印成功回调中的数据。

4 个解决方案

#1


4  

Try this

<script type="text/javascript">
        $( "#indexform" ).on( "submit", function( event ) {
              event.preventDefault();
              console.log( $(this).serialize() );
              var formdata = $(this).serialize();
              $.ajax({
                    type:"POST",
                    url:"result.php",
                    data:formdata,
                    success: function(html){
                        alert(html);
                    }
              });
            });
        </script>

In your result.php page

在你的result.php页面中

 $name=$_REQUEST['fname'];
    $email=$_REQUEST['email'];
    echo $name." ".$email;

#2


2  

You are setting the dataType to json so you must ensure that you only return valid json.

您正在将dataType设置为json,因此您必须确保只返回有效的json。

That means you cannot echo or print whatever you want; you should collect your data in an array or object and then only once output that like:

这意味着你无法回复或打印任何你想要的东西;你应该在数组或对象中收集数据,然后只输出一次,如:

echo json_encode($your_data);

#3


0  

Echo the content inside results.php page.

回显results.php页面内的内容。

Ex:

echo "two fields $first_field , $second_field  successfully inserted. " ;

This content results as an ajax response and stores in the data variable .

此内容作为ajax响应生成,并存储在数据变量中。

#4


0  

Hello My Friends your html code is in

你好我的朋友你的HTML代码是

<script type="text/javascript">
    $( "#indexform" ).on( "submit", function( event ) {
          event.preventDefault();
          console.log( $(this).serialize() );
          var formdata = $(this).serialize();
         // alert(formdata);
          $.ajax({
                type:"POST",
                url:"result.php",
                dataType:'json',
                data:formdata,
                success: function(data){
                    alert(data);
                }
          });
        });
    </script>

and you result.php file code is in

你的result.php文件代码是在

<?php
    include 'db.php';
    $name=$_REQUEST['fname'];
    $email=$_REQUEST['email'];
    echo $name." ".$email;
?>

now the result.php file is return the entered name and email address.

现在result.php文件返回输入的名称和电子邮件地址。

#1


4  

Try this

<script type="text/javascript">
        $( "#indexform" ).on( "submit", function( event ) {
              event.preventDefault();
              console.log( $(this).serialize() );
              var formdata = $(this).serialize();
              $.ajax({
                    type:"POST",
                    url:"result.php",
                    data:formdata,
                    success: function(html){
                        alert(html);
                    }
              });
            });
        </script>

In your result.php page

在你的result.php页面中

 $name=$_REQUEST['fname'];
    $email=$_REQUEST['email'];
    echo $name." ".$email;

#2


2  

You are setting the dataType to json so you must ensure that you only return valid json.

您正在将dataType设置为json,因此您必须确保只返回有效的json。

That means you cannot echo or print whatever you want; you should collect your data in an array or object and then only once output that like:

这意味着你无法回复或打印任何你想要的东西;你应该在数组或对象中收集数据,然后只输出一次,如:

echo json_encode($your_data);

#3


0  

Echo the content inside results.php page.

回显results.php页面内的内容。

Ex:

echo "two fields $first_field , $second_field  successfully inserted. " ;

This content results as an ajax response and stores in the data variable .

此内容作为ajax响应生成,并存储在数据变量中。

#4


0  

Hello My Friends your html code is in

你好我的朋友你的HTML代码是

<script type="text/javascript">
    $( "#indexform" ).on( "submit", function( event ) {
          event.preventDefault();
          console.log( $(this).serialize() );
          var formdata = $(this).serialize();
         // alert(formdata);
          $.ajax({
                type:"POST",
                url:"result.php",
                dataType:'json',
                data:formdata,
                success: function(data){
                    alert(data);
                }
          });
        });
    </script>

and you result.php file code is in

你的result.php文件代码是在

<?php
    include 'db.php';
    $name=$_REQUEST['fname'];
    $email=$_REQUEST['email'];
    echo $name." ".$email;
?>

now the result.php file is return the entered name and email address.

现在result.php文件返回输入的名称和电子邮件地址。