使用AJAX将数据发送到.php文件,如何从中获取数据?
16lz
2021-01-22
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'
});
}
});
});
});
更多相关文章
- PHP会话变量没有转移到我登录的页面,但是会话ID是
- php变量函数,回调函数
- php中static 静态变量和普通变量的区别
- 您是否认为PHP中的错误形式是在类方法中访问超级全局变量?
- 040-PHP使用闭包函数来进行父实例的变量自增,正确示例
- 如何使用变量访问多维数组中的某些元素?
- 只有变量可以通过引用- array_walk传递。
- 如果我们通过引用传递,是不是创建了变量?
- 如果key在变量中,PHP如何从数组中获取值