如何根据url触发ajax调用

时间:2014-12-26 19:47:02

标签: javascript jquery ajax wordpress

我正在建立优惠券网站。我需要根据页面的url触发ajax动作。让我解释。

例如,如果用户转到页面website_url /?coupon_id = 99 - 他获取页面website_url并在其中弹出ajax动作(ajax获取id = 99的优惠券帖子类型的数据并显示它的值) 。

如果用户访问网页website_url / page1 /?coupon_id = 99 - 他获取页面website_url / page1 /和相同的弹出窗口。

您可以在某些优惠券网站上看到此逻辑,例如,coupondunia.in

我创建了ajax动作,它正在运作

function coupon_get_code(){
    $couponid = $_GET['couponid'];  
    $code = get_post( $couponid );
    if( !empty( $code ) ){
        $offer_coupon = get_post_meta( $code->ID, 'coupon', true );
        $response .= '<div class="coupon_modal_coupon">'.$offer_coupon.'</div>';
    }
    else{
        $response = __( 'Offer does not exists', 'textdomain' );
    }
    echo  $response ;
    die;
}
add_action('wp_ajax_ajax_code', 'coupon_get_code');
add_action('wp_ajax_nopriv_ajax_code', 'coupon_get_code');

目前我根据点击触发了ajax动作,就像这样

    // Coupon Modal
  $( '.offer_coupon.masked_coupon:not(.expired_coupon)' ).live("click", function(e){
    var $this = $(this);
    var couponid = $this.data('couponid'); 

    $.pgwModal({
        url: translation.ajax_url + "?action=ajax_code&couponid=" + couponid,
        titleBar: false,
        ajaxOptions : {
            success : function(response) {
                if (response) {
                    $.pgwModal({ pushContent: response });
                } else {
                    $.pgwModal({ pushContent: 'An error has occured' });
                }                
            }
        }
    });

  }); 

但是如何基于url触发这个ajax请求?

2 个答案:

答案 0 :(得分:1)

您可以从页面加载的URL中获取最后一个数字字符(等于优惠券ID),而不是从数据属性中单击获取它。

$(window).on('load', function() {

  // Get the last numbers from the current page URL using Regex
  var couponid = window.location.href.match(/\d+$/);

  $.pgwModal({
    url: translation.ajax_url + "?action=ajax_code&couponid=" + couponid,
    titleBar: false,
    ajaxOptions : {
        success : function(response) {
            if (response) {
                $.pgwModal({ pushContent: response });
            } else {
                $.pgwModal({ pushContent: 'An error has occured' });
            }                
        }
    }
 });

});

将jQuery放在名为ajax-coupon.js的文件中,并有条件地将脚本排入队列。

// Conditionally enqueue your script only if the template coupons.php is used to display the page.
function my_scripts_method() {

  // Register your script location, dependencies and version
  wp_register_script('ajax-coupon', get_template_directory_uri() . '/js/ajax-coupon.js', array('jquery'), '1.0' );

  // Check if the current page is using coupons.php, if so, load the script.
  if ( is_page_template( 'coupons.php' ) ) {
    wp_enqueue_script('ajax-coupon');
  }

}

add_action('wp_enqueue_scripts', 'my_scripts_method');

答案 1 :(得分:0)

我想我找到了可以帮助我的功能。现在测试。

  function GetURLParameter(sParam){
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
      var sParameterName = sURLVariables[i].split('=');
      if (sParameterName[0] == sParam) 
      {
        return sParameterName[1];
      }
    }
  }

所以我的ajax触发器可以像这样

  var coupontrigger = GetURLParameter("couponid");
  if(coupontrigger){
    $.pgwModal({
        url: translation.ajax_url + "?action=ajax_code&couponid=" + coupontrigger,
        titleBar: false,
        ajaxOptions : {
            success : function(response) {
                if (response) {
                    $.pgwModal({ pushContent: response });
                } else {
                    $.pgwModal({ pushContent: 'An error has occured' });
                }
            }
        }
    });
  };
相关问题