我想在我的应用程序中创建一个联系表单,该表单使用Jquery mobile 1.4 + phonegap-build 3.3+,供用户与我联系。
但是我不确定如何发送消息... Ajax请求到服务器脚本,或者可能带有Phonegap插件?我有以下3个条件:
我不希望用户从应用程序中移出至他的电子邮件应用程序。因此,我想通过点击“发送”按钮从应用程序内发送消息,仅此而已。
如果用户处于脱机状态,我还需要存储该消息,并在手机恢复在线状态时稍后再发送。
我还需要获取用户的电子邮件地址,以便能够回答他。
第一种选择:像emailComposer这样的插件在这里吗?
第二种选择:对服务器脚本的请求?
什么是最好的?
谢谢
正如您在注释中请求客户端-服务器脚本示例一样,这是一个使用jquery ajax调用服务器中php脚本的示例。
Cleint / phonegap应用程序:
$('#sendlink').on("vclick", function(e) {
e.preventDefault();
var data={
userEmail: userEmail,
subject: subject,
destEmail: destEmail,
body: body
}
function sendToServer() {
return $.ajax({
url: 'http://yoururl.com/mail.php',
type: "POST",
dataType: "xml",
data: data
});
}
sendToServer().done(handleSuccess);
sendToServer().fail(handleError);
function handleSuccess(data /* , textStatus, jqXHR */ ) {
console.log("Message successfully sent");
}
function handleError(data , textStatus, jqXHR ) {
console.log("Error when sending the message : ");
console.log(data);
console.log(data.response);
console.log(textStatus);
console.log(jqXHR);
}
});
服务器端:
<?php
#if you also need this script to respond to other domains, add those header lines
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: *");
if($_POST) {
$userEmail=$_POST['userEmail'];
$subject=$_POST['subject'];
$destEmail=$_POST['destEmail'];
$body=$_POST['body'];
$response = mail($destEmail, $subject, $body, "From:" . $userEmail);
echo $response;
exit();
}
?>
这是一个例子。尝试搜索对服务器的ajax调用,并了解如何发送和接收xml响应。在php脚本中,您将需要发送一些xml响应。或者,您可以使用JSON而不是XML。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句