我编写了一个简单的ajax请求,但它一直给我一个状态== 0错误。我检查过var。它会一直存在,直到验证结束。 ajax运行之前发送,因为我看到发生在开发控制台然后它给出一个状态== 0错误..我目前正在开发XAMPP并且有点相同类型的验证正在工作,但这不是..
这是JS
function contact_validations() {
var name = jQuery('#contactname').val();
var cemail = jQuery('#contactemail').val();
var tel = jQuery('#contactphone').val();
var address = jQuery('#contactaddress').val();
var message = jQuery('#contactsubject').val();
var s = true;
if (name == '' || name == null) {
jQuery('#name_err').html('Please enter your name');
s = false;
} else {
jQuery('#name_err').html('');
}
if (cemail == '') {
jQuery('#email_err').html('Please enter email');
s = false;
} else {
var x = cemail;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
jQuery('#email_err').html("Please Enter Valid E-mail Address");
s = false;
} else {
jQuery('#email_err').html('');
}
}
if (tel == '' || tel == null) {
jQuery('#tel_err').html('Please enter your telephone number');
s = false;
} else {
jQuery('#tel_err').html('');
}
if (address == '' || address == null) {
jQuery('#address_err').html('Please enter your address');
s = false;
} else {
jQuery('#address_err').html('');
}
if (message == '' || message == null) {
jQuery('#message_err').html('Please enter your message');
s = false;
} else {
jQuery('#message_err').html('');
}
if (s) {
var url = "/c/wp-admin/admin-ajax.php";
jQuery.ajax({
type: "POST",
url: url,
data: "action=contact_email&name=" + name + "&email=" + cemail + "&tel=" + tel + "&address=" + address + "&message=" + message,
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
},
beforeSend: function () {
console.log('happened');
jQuery('#ajax-loader').show();
jQuery('#ajax-loader').html('<img src="/c/wp-content/themes/aussie/images/ajax-loader.gif">');
},
success: function (result) {
jQuery('#ajax-loader').hide();
console.log(result);
if (result == '0') {
console.log("No Result")
}
if (result == '1') {
jQuery.fancybox({
href: '#contact-popup'
});
jQuery('#contactname').val('');
jQuery('#contactemail').val('');
jQuery('#contactphone').val('');
jQuery('#contactaddress').val('');
jQuery('#contactsubject').val('');
console.log(s);
}
}
});
}
}
这是PHP
function contact_email() {
$url = get_bloginfo('url');
$storeemail = get_option('contactemailstored');
$to = $storeemail['contactemail'];
$subject = $url . ' - Contact';
$name = $_POST['contactname'];
$email = $_POST['contactemail'];
$phone = $_POST['contactphone'];
$address= $_POST['contactaddress'];
$content= $_POST['contactsubject'];
$message = '<br>'.'Name:- ' . $name . '<br>';
$message .= 'Email- ' . $email . '<br>';
$message .= 'Phone:- ' . $phone . '<br>';
$message .= 'Address :' . $address . '<br>';
$message .= 'Subject :' . $content . '<br>';
$sent_message = wp_mail( $to , $subject, $message );
if ( $sent_message ) {
// the message was sent...
echo '1';
} else {
// the message was not sent...
echo '0';
}
exit;
}
add_action('wp_ajax_contact_email', 'contact_email');
add_action('wp_ajax_nopriv_contact_email', 'contact_email');
编辑:
HTML,如果有人需要
<form name="contactemail_form" action="" method="post" class="wpcf7-form">
<input type="hidden" name="contactemail_form_submitted" value="Y">
<div class="contact-form">
<div class="form-row">
<div class="left">
<span class="control-wrap ">
<input type="text" name="contactname" id="contactname" value="" size="40" class="form-control text" placeholder="Name">
</span>
<span class="enq_err" id="name_err"></span>
</div>
<div class="right">
<span class="control-wrap ">
<input type="text" name="contactemail" id="contactemail" value="" size="40" class="form-control email" placeholder="Email">
</span>
<span class="enq_err" id="email_err"></span>
</div>
</div>
<div class="form-row">
<div class="left">
<span class="control-wrap">
<input type="tel" name="contactphone" id="contactphone" value="" size="40" class="wpcf7-form-control text " placeholder="Phone">
</span>
<span class="enq_err" id="tel_err"></span>
</div>
<div class="right">
<span class="control-wrap ">
<input type="text" name="contactaddress" id="contactaddress" value="" size="40" class="wpcf7-form-control text" placeholder="Address">
</span>
<span class="enq_err" id="address_err">
</div>
</div>
<div class="form-row">
<div class="full-width">
<span class="control-wrap ">
<textarea name="contactsubject" id="contactsubject" cols="40" rows="10" class="form-control textarea" placeholder="How Can We Help You?"></textarea>
</span>
<span class="enq_err" id="message_err"></span><br>
<input type="submit" value="send request" class="form-control" name="contactemail_submit" onclick="return contact_validations();">
<div id="ajax-loader" class="fancybox"></div>
<div style="display:none;">
<div class="aussi-en-pop" id="contact-popup"><h3>Thank you for contacting us.</h3> <p>Your <?php echo get_bloginfo( 'name' )?> agent will respond to your enquiry within 48 hours.</p></div>
</div>
</div>
</div>
</div>
</form>
答案 0 :(得分:0)
好的解决方案对我有用。我不知道为什么它导致错误,但我尝试将数据格式更改为正确的JSON。这是更新的js。
function contact_validations(){
event.preventDefault();
var name= jQuery('#contactname').val();
var cemail= jQuery('#contactemail').val();
var tel= jQuery('#contactphone').val();
var address= jQuery('#contactaddress').val();
var message= jQuery('#contactsubject').val();
var s=true;
if(name==''|| name==null ){
jQuery('#name_err').html('Please enter your name');
s=false;
}else{
jQuery('#name_err').html('');
}
if(cemail==''){
jQuery('#email_err').html('Please enter email');
s=false;
}else{
var x=cemail;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{ jQuery('#email_err').html("Please Enter Valid E-mail Address"); s= false;}
else{
jQuery('#email_err').html('');
}
}
if(tel==''|| tel==null ){
jQuery('#tel_err').html('Please enter your telephone number');
s=false;
}else{
jQuery('#tel_err').html('');
}
if(address=='' || address==null){
jQuery('#address_err').html('Please enter your address');
s=false;
}else{
jQuery('#address_err').html('');
}
if(message=='' || message==null){
jQuery('#message_err').html('Please enter your message');
s=false;
}else{
jQuery('#message_err').html('');
}
console.log( url );
if(s)
{
var url = "http://localhost:9080/c/wp-admin/admin-ajax.php";
var data = {
"action":"contact_email" ,
"name": name ,
"email": cemail ,
"tel": tel ,
"address": address ,
"message": message
}
jQuery.ajax({
type: "POST",
url: url,
contentType: 'JSON',
data: JSON.stringify(data),
beforeSend: function(){
console.log('happened');
jQuery('#ajax-loader').show();
jQuery('#ajax-loader').html('<img src="http://localhost:9080/c/wp-content/themes/aussie/images/ajax-loader.gif">');
},
success: function(result) {
jQuery('#ajax-loader').hide();
console.log(result);
if(result=='0')
{
console.log("No Result")
}
if(result=='1')
{
jQuery.fancybox({
href: '#contact-popup'
});
jQuery('#contactname').val('');
jQuery('#contactemail').val('');
jQuery('#contactphone').val('');
jQuery('#contactaddress').val('');
jQuery('#contactsubject').val('');
console.log ( s );
}
}
});
}
}