2014-06-12 3 views
2

AJAX에 문제가 있습니다.AJAX Wordpress url

jQuery.ajax({ 
    type:"POST", 
    url: "/wp-admin/admin-ajax.php", // our PHP handler file 
    data: "myDataString", 
    success:function(results){ 
     alert(results); 
    } 
)}; 

이 요청은 작동하지 않습니다. WP에서 동작에 맞춤 URL을 추가하려면 어떻게해야하나요?

+0

무슨 당신의 목표를 만들 수? 로그인 후 데이터를 가져 오시겠습니까? –

+0

아니요, 자동 응답하지 않고 아약스 요청을 원합니다. –

+0

권한없이 데이터를 넣으시겠습니까? –

답변

1

질문은 매우 명확하지 않다, 그러나 나는 모두가 기록과 비는 로그인 한 사용자를 위해 당신이 AJAX 작업을 추가하는 데 도움이됩니다. 지금은

// Add this to functions.php 
// Let's enqueue our javascript file 
function include_ajax_js() { 

    wp_register_script(
     'ajax-custom-script', 
     get_stylesheet_directory_uri() . '/js/ajax.js', 
     array('jquery') 
    ); 

    // Add data your script that you can normally 
    // only get from the server side of WordPress. 

    $localize = array(
     'ajaxurl' => admin_url('admin-ajax.php'), 
     // Securing your WordPress plugin AJAX calls using nonces 
     'auth' => wp_create_nonce('_check__ajax_100') 
    ); 
    wp_localize_script('ajax-custom-script', 'custom_ajax_vars', $localize); 

    wp_enqueue_script('ajax-custom-script'); 

} 
add_action('wp_enqueue_scripts', 'theme_name_scripts'); 

// Let's make function to handle AJAX requests 
function my_ajax_action() { 

    // Check nonce 
    check_ajax_referer('_check__ajax_100', 'nonce_field'); 

    // Let's display some useful information 
    // May be you need to check if the user is still logged in 

    $user_greeting = is_user_logged_in() ? 'Hello User !' : 'Hello Guest !'; 
    $data = array('msg' => $user_status); 

    echo json_encode($data); // encode the output to JSON 
    die(); 
} 
add_action('wp_ajax_check_status', 'my_ajax_action'); 
// wp_ajax_nopriv_ executes for users that are not logged in 
add_action('wp_ajax_nopriv_check_status', 'my_ajax_action'); 

충분히 PHP,의는은 ajax.js

jQuery(document).ready(function(){ 

    jQuery.ajax({ 
     type:"POST", 
     // We can easily get the AJAX Url from the object we've created before 
     url: custom_ajax_vars.ajaxurl, 
     // Data should be sent as an object 
     data: { action: check_status, nonce_field: custom_ajax_vars.nonce }, 
     success:function(data){ 
      alert(data.status); 
     } 
    )} 
}) 
}) 
+0

워드 프레스에서 우리는이 액션을 어떤 액션이라도 호출 할 수 있습니다 : 'add_action ("wp_ajax_nopriv_action_name", "action_name"); –

0

이 시도 :

var username = "user123"; 
var password = "pass123"; 

//var username = ""; 
//var password = "";  
var headers = {}; //list of headers 

if(username && password) //user and pass exists 
    headers['Authorization'] = "Basic " + btoa(username + ":" + password); 

    jQuery.ajax({ 

    type:"POST", 

    url: http://siteurl.com/wp-admin/admin-ajax.php, 

    data: "myDataString", 
    headers: headers, //use our headers 
    success:function(results){ 

     alert(results); 

    } 

    )};