2012-01-20 4 views
1

Wordpress/Buddypress에서 간단한 ajax 테스트를 시도하고 있습니다.Wordpress Ajax Firefox 응답 없음

아래 코드는 IE와 Chrome에서 잘 작동합니다. Firefox에서는 자바 스크립트가 호출되었지만 현재 페이지가 새로 고침됩니다. 방화 켓 콘솔에 오류가 없지만 응답이 비어 있습니다. PHP가 호출되고 있다고 생각하지 않습니다.

Firefox에서이 기능을 사용하려면 무엇을 변경해야합니까?

자바 스크립트 :

jQuery(document).ready(function(){ 

    jQuery('#test-form input#save').click(function(){ 
      jQuery('.ajax-loader').toggle(); 

      jQuery('#save').attr({'disabled': true}); 

      jQuery.post(ajaxurl, { 
        action: 'test_save', 
        'cookie': encodeURIComponent(document.cookie), 
        '_wpnonce': jQuery("input#_wpnonce-save").val(), 
      }, 
        function(response) {    
          jQuery('.ajax-loader').toggle(); 
      // alerts work up to here in all browsers 
          jQuery('#test-form').append(response); 
        }); 
      }); 

});

PHP는 :

이 FF, 크롬과 IE에서 작동
add_action('bp_after_profile_loop_content', 'show_test', 100);  
add_action('wp_ajax_test_save', 'test_save_function'); 

function show_test() { 
?> 
<form action="#" id="test-form"> 
    <?php wp_nonce_field('save', '_wpnonce-save'); ?> 
    <input type="submit" name="save" id="save" value = "test ajax"/> 
    <span class="ajax-loader"></span> 
</form> 

<?php 
} 

function test_save_function() { 
check_ajax_referer('save'); 
echo "php -button was clicked"; 
} 

답변

1

. 또한 - die()를 추가하십시오. PHP 함수의 끝에서

jQuery(document).ready(function(){ 

    jQuery('#test-form input#save').click(function(){ 
      jQuery('#save').attr({'disabled': true}); 

     jQuery.ajax({ 
      type: 'POST', 
      url:"/wp-admin/admin-ajax.php", 
      data: { 
       action: 'test_save', 
     '_wpnonce': jQuery("input#_wpnonce-save").val(), 
      }, 
      success: function(results) { 
     jQuery('#save').attr('value', 'js was Clicked'); 
       jQuery('#test-form').append(results); 
      } 
    }); 
    return false; 

    }); 
}); 
2

시간에 WordPress에 로그온 한 경우에만 작동합니다! 로그인하지 않은 경우 wp_ajax은 후크 wp_ajax_nopriv_myfunction을 호출합니다.

링크 :

https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action) http://www.simonbattersby.com/blog/2012/06/wordpress-wp_ajax-returning-0-error/

희망이 시간을 사람을 저장하거나 그래서 ...

+0

는 "로그인되어 있지 않을 경우, 후크 wp_ajax_nopriv_myfunction를 호출 wp_ajax." 그것은 ** 열쇠 **였습니다. 고마워요! – Arsalan

+0

감사합니다. 새로운 아약스 + wordpress 하고이 데프 나를 좀 시간을 절약 :) –