如何将JSON传回PHP的AJAX请求?
I've built a simple hangouts application that notifies me when someone launches a hangout from on my webpage - outside of Google plus.
我已经构建了一个简单的hangouts应用程序,当有人在我的网页上发布hangout时——在谷歌plus之外。
I'm at the stage where I've called my server side script that notifies me that a new user has initiated a hangout and I need to pass a message back to the hangout to display an updated prompt to the users. Something along the lines of - A support engineer has been alerted to this hangout and will be with you shortly.
我正在调用我的服务器端脚本,它通知我一个新用户已经启动了一个hangout,我需要将消息传回hangout以向用户显示更新的提示符。一些类似的东西——一个支持工程师已经被提醒到这个hangout,并且很快就会和你在一起。
I've read in the hangout data, including who has joined from the hangouts api and passed this to my server side code no problems. But I'm not sure how I pass information back to my hangout app via its ajax request.
我已经阅读了hangout数据,包括谁加入了hangouts api并将它传递给了我的服务器端代码,没有问题。但我不确定如何通过ajax请求将信息传回hangout应用程序。
Here's my ajax request using jQuery.
这是我使用jQuery的ajax请求。
var hangoutUrl = gapi.hangout.getHangoutUrl();
var personsInHangout = gapi.hangout.getParticipants();
var callbackUrl = 'http://styxofdynamite.bitnamiapp.com/notify.php?';
$.ajax({
url: callbackUrl,
dataType: 'text',
data: {
"personsInHangout" : personsInHangout,
"hangoutUrl" : hangoutUrl,
"topic" : params['gd'],
}
}).done( function(data, status, xhr){
//call was made process result
$('.msg').html(data.msg);
}).fail( function(xhr, status, error){
$('.msg').html("There was a problem contacting the server. (" + status + ")");
});
At the moment I'm not returning anything so I'm happily hitting the .fail()
ideally I need to return something from notify.php back to this hangout application.
现在我没有返回任何东西,所以我很高兴地点击了。fail(),理想情况下,我需要返回一些通知。php回到这个hangout应用程序。
Sever side snippet:
服务器端代码片段:
$personsInHangout = $_GET['personsInHangout'];
$hangoutUrl = $_GET['hangoutUrl'];
$topic = $_GET['topic'];
$emailMessage = 'Hey Support Team, ';
for($i = 0, $size = count($personsInHangout); $i < $size; ++$i) {
$emailMessage .= $personsInHangout[$i]['person']['displayName'];
$emailMessage .= ' is currently waiting in the support ';
$emailMessage .= '<a href="'.$hangoutUrl.'">hangout</a>';
$emailMessage .= ' to discuss ' . $topic;
}
print $emailMessage;
$to = 'support@team.com';
$subject = 'New Hangout Request';
$headers = 'From: hangout.monitor@team.com' . "\r\n" .
'Reply-To: hangout.monitor@team.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $emailMessage, $headers);
//return something to say that a support engineer has been notified.
3 个解决方案
#1
3
Use php's built in json_encode
docs to send back a JSON formatted response. You will also need to send some headers in addition to the data.
使用json_encode文档中内置的php返回JSON格式的响应。除了数据之外,还需要发送一些标题。
<?php
// This tells the browser the following data should be interpreted as JSON, as opposed to text or html
header('Content-Type: application/json');
// Take any $data and format it correctly as JSON. Works with arrays, strings, numbers
echo json_encode($data);
?>
更多相关文章
- PHP - Content-type未指定假设应用程序/x-www-form-urlencode。
- 在php和servlet应用程序之间进行通信
- 使用PHP和MySQL开发“测试”Web应用程序的数据库设计
- 数据库是存储我的Web应用程序数据的最佳选择吗?
- PHP获取服务器端的相关信息
- 架构比MVC更适合Web应用程序?
- 使用ajax在服务器端恢复json时出错
- php清理当前目录下的指定文件和空目录(源码),建议服务器端执行
- 保存在Java桌面应用程序应用程序和网站上使用的个人用户设置的最