如何根据 url 触发 ajax 调用
How to trigger ajax call based on 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 请求呢?
您可以在页面加载时从网址中获取最后一个数字字符(等于优惠券 ID),而不是在点击时从 data 属性获取它。
$(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');
我想我找到了可以帮助我的函数。现在测试。
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' });
}
}
}
});
};
相关文章:
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Javascript/jQuery中的并行Ajax调用
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- node.js请求数据事件未在CORS ajax调用中触发
- 如何从ajax调用返回.wrap()元素
- Ajax调用在Firefox中不会自动响应
- AJAX调用中的非法调用
- 使用AJAX调用将GeoJSON数据拉入传单
- 在MVC 4中,对Controller的Ajax调用为空
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 在ajax调用中阻止来自JS对象的函数
- 如何在MVC5中ajax调用的部分视图中引用外部javascript
- 页面加载后的Ajax调用
- 如何使用Ajax调用特定的控制器和操作来重定向用户
- curl和fileget_contents在ajax调用后不起作用
- 在任何AJAX调用之前触发一个javascript函数
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- Ajax调用ERror Cross Origin REquest:在rails中自动完成大学列表
- ASP.NET MVC 3-在ajax调用后,重定向到新页面或生成页面刷新
- 点击功能没有'ajax调用动态元素(Backbone)后无法工作