problem using ajax url

时间:2013-01-22 作者:daco.marseille

我有一个js 文件如下:

var category = [];
var genre = [];
var type = [];
var mainContent = jQuery(\'#content\');
var siteURL ="http://" + top.location.host.toString();    
var URL = siteURL + "/?category="+ category +"&genre=" + genre +"&type="+ type +" #content";

jQuery(\'.ajax-cb\').each(function() {
    jQuery(this).click(function() {         
        mainContent.load(URL,function(){                    
            mainContent.animate({opacity: \'1\'});
        });                   
    });     
}); 
它确实加载GET[] 用于更改循环的参数,

在主题的function.php 我有:

function pre_selected_results() {
  $taxquery = array(
      \'relation\' => \'OR\',
      array(
        \'taxonomy\' => \'category\',
        \'field\' => \'slug\',
        \'terms\' => $_GET[\'category\'],
        \'operator\'=>\'IN\'
      ),
      array(
        \'taxonomy\' => \'genres\',
        \'field\' => \'slug\',
        \'terms\' => $_GET[\'genre\'],
        \'operator\'=>\'IN\'
      ),
      array(
        \'taxonomy\' => \'types\',
        \'field\' => \'slug\',
        \'terms\' => $_GET[\'type\'],
        \'operator\'=>\'IN\'
      )                                         
  );
  $stack[] = "dog";
  $the_query = new WP_Query($myquery);
  while ($the_query->have_posts()) : 
  //the loop

  //I then collect info from the loop displayed          
  $args = array(\'orderby\' => \'name\', \'order\' => \'ASC\', \'fields\' => \'slugs\');
  $results = wp_get_post_terms(get_the_ID(),\'category\',$args);  
      foreach ($results as $result){
          array_push($stack, $result);
      }
  endwhile;
/*And I use wp_localize_script(); to send $stack back to js*/
  wp_enqueue_script( \'feedback\' );
  wp_localize_script( \'feedback\', \'jsdata\', $stack);
}; //end of pre_selected_results
在我的主页上,我呼叫pre_selected_results(); 循环显示良好并刷新,

但是$stack 仅在编辑URL时刷新GET[] 在浏览器中手动,

否则,它只会返回“狗”,有什么明显的东西是我遗漏的吗?

1 个回复
SO网友:Tom J Nowell

您应该使用WP AJAX API,而不是在您的主模板中重新创建它们,例如:

功能。php:

// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( \'my-ajax-request\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );

add_action(\'wp_ajax_my_action\', \'my_action_callback\');
add_action(\'wp_ajax_nopriv_my_action\', \'my_action_callback\');

function my_action_callback() {
    $content = \'something to send back to the browser\';

    echo $content;

    die(); // this is required to return a proper result
}
Javascript:

jQuery.post(
    // see tip #1 for how we declare global javascript variables
    MyAjax.ajaxurl,
    {
        // here we declare the parameters to send along with the request
        // this means the following action hooks will be fired:
        // wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
        action : \'myajax-submit\',

        // other parameters can be added along with "action"
        postID : MyAjax.postID
    },
    function( response ) {
        alert( response ); // 
    }
);
使用它,您可以绕过已损坏的代码,完全依赖标准化的API

您可以在此处阅读更多信息:

http://codex.wordpress.org/AJAX_in_Plugins

它也适用于主题

结束

相关推荐

How does admin-ajax.php work?

我们与外部开发人员有一些问题。我们想限制访问wp-admin 仅限现场内部访问(通过VPN). 这样就不会受到外部用户的攻击。我们可以从站点中枚举管理员,不希望他们被钓鱼。我们的开发人员说我们不能这样做,因为网站需要有外部访问的管理页面,这样页面才能正常工作。特别是admin-ajax 页什么是admin-ajax.php 页码do?它位于WordPress的管理部分。最终用户是否未经身份验证就访问?让外部用户使用此功能是否不安全?