我正在使用引導程序4測試版,並且我有一個我在html文件中創建的表單,該表單中有一個外部JavaScript文件鏈接在其中創建錯誤消息,然後指向成功的PHP文件。我也在使用this bootstrap validation plug in. 當試圖在本地查看和測試表單時,出現「跨源請求」錯誤。 這裏是錯誤的屏幕截圖 我已經查看了這意味着什麼,並且查找了一些試圖找到類似內容的問題,但是沒有任何回答我的問題。 基本上:有沒有辦法通過使php內部(在html中)並使javascript指向它來規避此問題?或者我可以以某種方式將JavaScript和PHP結合到一個外部文件中?如何處理php跨協議問題
和(這可能會使我的其他問題無關緊要)一旦我的網站託管,而不是本地查看,這個問題會消失嗎?
這裏是我的參考代碼: HTML:
<div class="row">
<div class="col-lg-12">
<form id="contactForm" name="sentMessage" class="" novalidate>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input class="form-control" id="name" type="text" placeholder="Your Name *" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input class="form-control" id="email" type="email" placeholder="Your Email *" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" id="message" placeholder="Your Message *" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button id="sendMessageButton" class="btn btn-xl" type="submit">Send Message</button>
</div>
</div>
</form>
</div>
</div>
的javascript:
$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$this = $("#sendMessageButton");
$this.prop("disabled", true); // Disable submit button until AJAX call is complete to prevent duplicate messages
$.ajax({
url: "contact_me.php",
type: "POST",
data: {
name: name,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append($("<strong>").text("Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!"));
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
complete: function() {
setTimeout(function() {
$this.prop("disabled", false); // Re-enable submit button when AJAX call is complete
}, 500);
}
});
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
PHP:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected]
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
我假設你正在查看保存在你的計算機上的html文件(也許有一個類似'file:// path/to/file.html'的URL)。如果是這樣的話,你應該會得到一個不同的錯誤,因爲你沒有運行服務器來處理PHP請求。如果您使用服務器來託管這些文件,那麼您應該仍然可以,因爲請求設置爲「contact_me.php」,它將使用與HTML頁面相同的域。底線是,我不知道爲什麼你會得到這個錯誤。你能發佈確切的錯誤信息嗎? – Sidney
https://stackoverflow.com/a/9866124/1064767 – Sammitch
@Sidney我爲錯誤的問題添加了截圖!是的,我正在查看保存在我的電腦上的html文件。 – Ailis