Ajax调用在Wordpress中添加当前URL [英] Ajax call add current url in wordpress

查看:78
本文介绍了Ajax调用在Wordpress中添加当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在wordpress js文件中使用jquery执行ajax调用时,它会自动重定向到当前路径,然后附加我的自定义路径,因此我无法重定向我的真实URL.

When I am trying to perform an ajax call using jquery in wordpress js file, it automatically redirects to current path and then appends my custom path so I can't redirect my true URL.

这是我的代码:

var path_test = document.location.hostname + '/wpcontent/themes/expression/imagecount.php';
var path;
$.ajax({
   url: path_test,
  type: "GET",
  data: '30'
 }).done(function() {
    alert('ajax call success');
});

它添加了路径,但是首先添加了当前URL,然后添加了我的URL,因此ajax调用失败.

It adds path, but first adds current URL then adds my URL so ajax call fails.

推荐答案

对于任何ajax调用,您都应参考编解码器.

For any ajax calls you should refer to the codex.

  1. https://codex.wordpress.org/AJAX_in_Plugins
  2. https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_% 28action%29
  1. https://codex.wordpress.org/AJAX_in_Plugins
  2. https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29

即使您没有在插件中运行它.

Even though if you are not running it in a plugin.

服务器端

add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );

function prefix_ajax_add_foobar() {
    // Handle request then generate response using WP_Ajax_Response

    // Here you would fetch and process desired file.
}

客户端

jQuery.post(
    ajaxurl, 
    {
        'action': 'add_foobar',
        'data':   'foobarid'
    }, 
    function(response){
        alert('The server responded: ' + response);
    }
);

在适当的设置下,您不想直接从主题文件夹中调用文件,最好有一个访问点,您可以加载所需的文件并返回正确的响应.

In a proper setting u dont want to call files directly from your theme folder, its better to have a single access point which you can load the file u want and return a proper response.

这篇关于Ajax调用在Wordpress中添加当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆