使用AJAX将数据发送到.php文件,如何从中获取数据?

时间:2022-10-08 17:24:35

I am using the following code to send a user id when the button with ID #share is pressed. But I don't know how to get data back, a variable, which when is true will run FB.ui.

当按下ID为#share的按钮时,我使用以下代码发送用户ID。但我不知道如何获取数据,一个变量,当为真时将运行FB.ui.

<script type="text/javascript">
    $(document).ready(function(){
        $("#share").click(function(){
            $.ajax({
                type: 'POST',
                url: 'upload.php',
                data: 'user_id=$user_id',
                success:      // I don't know what to put here, maybe "function(result){ if (result == true) {"?
                    FB.ui({
                        method: 'feed',
                        name: '$app_name',
                        link: '$app_url',
                        picture: '$upload_picture',
                        caption: 'Aluxian Apps',
                        description: '$app_description'
                    });
            });
        });
    });
</script>

4 个解决方案

#1


3  

Yes, you could json_encode the variable you want to send back to client.

是的,您可以json_encode您想要发送回客户端的变量。

echo json_encode( array('result' => true ) ); 

Then in your js

然后在你的js

$(document).ready(function(){
        $("#share").click(function(){
            $.ajax({
                type: 'POST',
                url: 'upload.php',
                dataType: 'json', 
                data: 'user_id=$user_id',
                success: function( data ) {
                   if( data.result ) // you can access the result variable here 
                    FB.ui({
                        method: 'feed',
                        name: '$app_name',
                        link: '$app_url',
                        picture: '$upload_picture',
                        caption: 'Aluxian Apps',
                        description: '$app_description'
                    });
               }
            });
        });
    });

#2


1  

<script type="text/javascript">
    $(document).ready(function(){
        $("#share").click(function(){
            $.ajax({
                type: 'POST',
                url: 'upload.php',
                data: 'user_id=$user_id',
                success:  function(data){ 
                    if (data.result == true) {
                      FB.ui({
                          method: 'feed',
                          name: '$app_name',
                          link: '$app_url',
                          picture: '$upload_picture',
                          caption: 'Aluxian Apps',
                          description: '$app_description'
                      });
                    }
            });
        });
    });
</script>

#3


1  

Your assumption is correct.

你的假设是正确的。

$.ajax({
    type: 'POST',
    url: 'upload.php',
    data: 'user_id=$user_id',
    success: function(result) {
        if (result == true) {
            FB.ui({
                method: 'feed',
                name: '$app_name',
                link: '$app_url',
                picture: '$upload_picture',
                caption: 'Aluxian Apps',
                description: '$app_description'
            });
        }
    }
});

#4


0  

Be sure to add a Content Type header to your PHP file so that it communicates the information back to the client correctly. At the top of your php file: header('Content type: application/json);

请务必在PHP文件中添加Content Type标头,以便将信息正确地传回客户端。在你的php文件的顶部:header('Content type:application / json);

#1


3  

Yes, you could json_encode the variable you want to send back to client.

是的,您可以json_encode您想要发送回客户端的变量。

echo json_encode( array('result' => true ) ); 

Then in your js

然后在你的js

$(document).ready(function(){
        $("#share").click(function(){
            $.ajax({
                type: 'POST',
                url: 'upload.php',
                dataType: 'json', 
                data: 'user_id=$user_id',
                success: function( data ) {
                   if( data.result ) // you can access the result variable here 
                    FB.ui({
                        method: 'feed',
                        name: '$app_name',
                        link: '$app_url',
                        picture: '$upload_picture',
                        caption: 'Aluxian Apps',
                        description: '$app_description'
                    });
               }
            });
        });
    });

#2


1  

<script type="text/javascript">
    $(document).ready(function(){
        $("#share").click(function(){
            $.ajax({
                type: 'POST',
                url: 'upload.php',
                data: 'user_id=$user_id',
                success:  function(data){ 
                    if (data.result == true) {
                      FB.ui({
                          method: 'feed',
                          name: '$app_name',
                          link: '$app_url',
                          picture: '$upload_picture',
                          caption: 'Aluxian Apps',
                          description: '$app_description'
                      });
                    }
            });
        });
    });
</script>

#3


1  

Your assumption is correct.

你的假设是正确的。

$.ajax({
    type: 'POST',
    url: 'upload.php',
    data: 'user_id=$user_id',
    success: function(result) {
        if (result == true) {
            FB.ui({
                method: 'feed',
                name: '$app_name',
                link: '$app_url',
                picture: '$upload_picture',
                caption: 'Aluxian Apps',
                description: '$app_description'
            });
        }
    }
});

#4


0  

Be sure to add a Content Type header to your PHP file so that it communicates the information back to the client correctly. At the top of your php file: header('Content type: application/json);

请务必在PHP文件中添加Content Type标头,以便将信息正确地传回客户端。在你的php文件的顶部:header('Content type:application / json);