我需要一点帮助。我设计了我的自定义 ajax 函数。这是一个简单的表单,输入 5 个值验证它们,然后通过 ajax 将数据发送到 php 函数,该函数通过电子邮件发送这些详细信息。成功后会向用户显示一个弹出窗口进行确认。
我已经应用了验证并且我也能够运行 ajax。但由于某种原因,我的成功函数一直返回 0。请帮忙。
提交 chrome 时的开发工具输出: http://imgur.com/m5Ah0md
在 chrome 上提交时的网络输出: http://imgur.com/R7q9WnH
我的javascript
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 );
}
}
});
}
}
我的Javascript:
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 '1';
}
exit;
}
add_action('wp_ajax_contact_email', 'contact_email');
add_action('wp_ajax_nopriv_contact_email', 'contact_email');
最佳答案
问题是您正在将 data
转换为 json,但 wp 需要一个名为 action
的字段来找到正确的函数,因此您的返回值为 0(未找到函数,没有遇到退出,所以返回 0)。
在这种情况下,您实际上不需要 json 值发送表单,但它会期望 json 响应,因此在您的 php 函数中您应该 json_encode
您的返回值。
jQuery(document).ready(function(){
var url = "<?php echo admin_url('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',
//JSON.stringify(data), -->will cause issue
data: data,
beforeSend: function(){
console.log('happened');
},
success: function(result) {
console.log(result);
}
});
});
PHP
function contact_email() {
// the message was sent...
echo json_encode(1); // if just sending 1, you can echo without json encode...
exit;
}
add_action('wp_ajax_contact_email', 'contact_email');
add_action('wp_ajax_nopriv_contact_email', 'contact_email');
正在发送 Json...
如果您愿意,可以将其他表单值转换为 json 并包含在其自己的对象键中。例如
var data={};
data.action= 'hook';
data= JSON.stringify(formData);
等等。
关于javascript - ajax成功返回0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33423727/