使用PHP

时间:2018-08-04 12:39:38

标签: javascript php jquery ajax google-app-engine

我正在开发一个已部署在Google App Engine上的项目,当用户提交联系表格时,我需要发送电子邮件。

我已经成功部署了我的应用程序,并向PHP文件实现了ajax请求,但此方法无法正常工作。

这是我的HTML代码:

<form action="" method="post">
      <div class="form-group">
           <input type="text" name="name" class="form-control input-field" placeholder="Name" required="">                    
            <input type="email" name="email" class="form-control input-field" placeholder="Email ID" required="">           
             <input type="text" name="subject" class="form-control input-field" placeholder="Subject" required="">                     
       </div>
       <textarea name="message" id="message" class="textarea-field form-control" rows="4" placeholder="Enter your message" required=""></textarea>
       <button type="submit" id="submit_btn" value="Submit" class="btn center-block">Send message</button>
</form>

这是Javascript: contact.js

    $("#submit_btn").click(function() { 

    var proceed = true;
    //simple validation at client's end
    //loop through each field and we simply change border color to red for invalid fields       
    $("#contact_form input[required], #contact_form textarea[required]").each(function(){
        $(this).css('background-color',''); 
        if(!$.trim($(this).val())){ //if this field is empty 
            $(this).css('background-color','#FFDEDE'); //change border color to #FFDEDE   
            proceed = false; //set do not proceed flag
        }
        //check invalid email
        var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; 
        if($(this).attr("type")=="email" && !email_reg.test($.trim($(this).val()))){
            $(this).css('background-color','#FFDEDE'); //change border color to #FFDEDE   
            proceed = false; //set do not proceed flag              
        }   
    });

    if(proceed) //everything looks good! proceed...
    {
        //get input field values data to be sent to server
        post_data = {
            'user_name'     : $('input[name=name]').val(), 
            'user_email'    : $('input[name=email]').val(), 
            'subject'       : $('input[name=subject]').val(), 
            'msg'           : $('textarea[name=message]').val()
        };

        //Ajax post data to server
        $.post('https://storage.googleapis.com/www.unmundonuevo.school/php/sendmail.php', post_data, function(response){  
            if(response.type == 'error'){ //load json data from server and output message     
                output = '<br><br><div class="error">'+response.text+'</div>';
            }else{
                output = '<br><br><div class="success">'+response.text+'</div>';
                //reset values in all input fields
                $("#contact_form input[required=true], #contact_form textarea[required=true]").val(''); 
                $("#contact_form").slideUp(); //hide form after success
            }
            $("#contact_results").hide().html(output).slideDown();
        }, 'json');
    }
});

这是我的 sendmail.php:

use \google\appengine\api\mail\Message;
if(isset($_POST['submit'])) {
    try {
        $email = 'arycloud7@gmail.com';
        $subject = 'A Test Subject';
        $mailBody = 'This is a test body.';
        $message = new Message();
        $message->setSender("abdul12391@gmail.com");
        $message->addTo($email);
        $message->setSubject($subject);
        $message->setTextBody($mailBody);
        $message->send();

        header("Location: /mail_sent");

    } catch (InvalidArgumentException $e) {

        $error = "Unable to send mail. $e";
    }
}

当我提交联系表时,它返回错误为: 在浏览器的控制台中,一些错误显示为:

  

[错误] Access-Control-Allow-Origin不允许起源http://www.unmundonuevo.school。   [错误]由于访问控制检查,XMLHttpRequest无法加载https://unnuevomundo-contact.appspot.com/sendmail.php。   [错误]无法加载资源:Access-Control-Allow-Origin不允许使用来源http://www.unmundonuevo.school。 (sendmail.php,第0行)

这些错误是什么意思?

出什么事了吗?

请救救我!

谢谢!

1 个答案:

答案 0 :(得分:0)

好像您需要配置Cross-Origin Resource Sharing(或CORS)。

您可以通过编写配置文件并将其应用于存储桶(如下所示)来实现:

gsutil cors set cors-json-file.json gs://example

cors-json-file.json:

[
    {
      "origin": ["http://example.appspot.com"],
      "responseHeader": ["Content-Type"],
      "method": ["GET", "HEAD", "DELETE"],
      "maxAgeSeconds": 3600
    }
]

您可以在the official documentation中获得更多信息。