JSON 成功消息 Shawn


json succes message shawn

我尝试确认电子邮件是用JSON发送的,然后使用ajax对事件进行警告,并在屏幕上显示成功消息。问题是电子邮件没有发送,我被重定向到"sended":true,因为php脚本的第一部分。html和css还可以,我已经测试了很多。我真正想要的是使用 php 发送电子邮件,而不是使用 ajax 显示成功或错误消息。问题是这是我第一次使用php,我只是找不到正确的方法。我不会问这个,但我必须在今晚之前交付项目,这是最后要做的事情。您可以在以下位置检查它的实际效果:http://webofdreams.ro/vworker/finemaid/finemaid.html# 在"给我们发送电子邮件"链接下。

.JS:

$ajax({
                     "type":"POST",
                     "url":"sendemail1.php",
                     "data": { name1: name1Val, emailFrom1: emailFrom1Val, comments: commentsVal},
                     "dataType":'json',
                     "success":function(response){
                         if (response.sended){
                            alert ("Mail Sended ok"); //Code after mail send
                           }else{
                            alert (response.error); //Code or allert on error
                           }
                          },                
                     error: function (xhr, ajaxOptions, thrownError){
                      alert(xhr.status+" "+thrownError);
                     }

.php:

$send = @mail($mailTo, $subject, $message, "From: ".$mailFrom1); 
if ($send){
    echo mail($mailTo, $subject, $message, "From: ".$mailFrom1) ? '{"sended":true}':'{"sended":false,"error":"Mail send fail."}';
}else{
 echo '{"sended":false,"error":"Request Error."}';
};

您的 ajax 请求充满了错误,所以我从头开始编写它,下面的脚本至少会处理 ajax 请求。

        $(function(){
        name1Val = 'CaptureName';
        emailFrom1Val = 'CaptureEmail';
        commentsVal = 'CaptureComments';
        parameters = 'name1=' + name1Val + '&emailFrom1=' + emailFrom1Val + '&comments=' + commentsVal;
        $.ajax({
            type: "POST",
            url: "sendmail.php",
            data: parameters,
            error: function(response){
                alert(response);
            },
            success: function(response){
                alert(response);
            }
        });
    });
首先,

我发现您的代码中存在错误

$.ajax({
 ^                    "type":"POST",
 |                    "url":"sendemail1.php",
 |                    "data": { name1: name1Val, emailFrom1: emailFrom1Val, comments: commentsVal},
                     "dataType":'json',
                     "success":function(response){
                         if (response.sended){
                            alert ("Mail Sended ok"); //Code after mail send
                           }else{
                            alert (response.error); //Code or allert on error
                           }
                          },   
         ----------->error: function (xhr, ajaxOptions, thrownError){
                      alert(xhr.status+" "+thrownError);
                    // should be 
         ----------->"error": function (xhr, ajaxOptions, thrownError){
                      alert(xhr.status+" "+thrownError);
                     }