wordpress:在插件管理区域中使用ajax时出错?

时间:2014-11-09 14:03:56

标签: jquery ajax wordpress

我正在使用admin-ajax在我的插件中进行ajax调用。因此,当出现错误时,我收到错误“relaod page”而没有显示错误。我无法看到日志,因为我无法访问服务器?我试过这样:

jQuery(function() {
   jQuery("#send_newsletter").click(function() {

      var subjecttitle = jQuery("#subjecttitle").val();
      var emailtosender = jQuery("#emailtosender").val();
      var post_id = jQuery("#post_id").val();
      var action = jQuery("#action").val();
      var dataString = 'emailtosender=' + emailtosender + '&action=' + action + '&subjecttitle=' + subjecttitle + '&post_id=' + post_id;


      jQuery("#flash").show();
      jQuery("#flash").fadeIn(400).html('<span class="loading">loading</span>');

      jQuery.ajax({
          type: "POST",
          url: "<?php plugins_url( 'newsletter/send.php'  ); ?>",
          data: dataString,
          cache: false,
          success: function(html) {
              jQuery("#display").after(html);
          }
      });

      return false;
  });
});

但是当我点击#send_newsletter发送时,它会重新加载页面,为什么!!

1 个答案:

答案 0 :(得分:0)

你需要阻止提交按钮的默认操作,但是你可能也遇到了你正在使用的网址的问题,我在你旁边留了一个便条

jQuery(function() {
jQuery("#send_newsletter").click(function(e) {

e.preventDefault();
var subjecttitle= jQuery("#subjecttitle").val();
var emailtosender= jQuery("#emailtosender").val();
var post_id= jQuery("#post_id").val();
var action = jQuery("#action").val();
var dataString = 'emailtosender='+ emailtosender+'&action='+ action+'&subjecttitle='+ subjecttitle+'&post_id='+ post_id;


jQuery("#flash").show();
jQuery("#flash").fadeIn(400).html('<span class="loading">loading</span>');

jQuery.ajax({
type: "POST",
url: "<?php plugins_url( 'newsletter/send.php'  ); ?>", // you will also have issues here you should use wp's ajaxurl and have the function in your functions file + hooked to wp-ajax...see http://www.smashingmagazine.com/2011/10/18/how-to-use-ajax-in-wordpress/ for more information
data: dataString,
cache: false,
success: function(html){
jQuery("#display").after(html);
});
} return false;
});
});