programing

AJAX 스크립트의 파일 경로(Wordpress)

coolbiz 2023. 3. 15. 23:19
반응형

AJAX 스크립트의 파일 경로(Wordpress)

다음 jquery-ajax 스크립트를 사용하여 이메일을 보냅니다.

    $.ajax({
        url: process.php,    
        type: "POST",
        data: data,        
        cache: false,
    ...

urle-메일을 보내는 php 파일을 호출하지만, ajax는 풀 경로를 지정한 경우에만 수신합니다.

url: "http://www.domain.com/wp-content/themes/site_theme/templates/process.php",

다음과 같은 구문을 사용해야 합니다.

url: "../../templates/process.php",

또는 변수를 사용하여 html 헤더/클라이언트에 선언합니다.

HTML

<script type="text/javascript">
  var urlMail = '<?php bloginfo('template_url'); ?>/templates/process.php';
</script>

대본

url: "../../templates/process.php",

단, 위의 두 경우 모두 브라우저 콘솔은 다음 오류를 가져옵니다.

POST http://www.domain.com/templates/process.php 404 Not Found 1.56s

어디가 틀렸지?

워드프레스에서 Ajax를 구현하는 방법은 아닙니다.모든 Ajax 요청은 다음 주소로 해야 합니다.admin-ajax.php.

템플릿 파일:

<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>

js:

$.ajax({
        url: ajaxurl,    
        type: "POST",
        cache: false,
        data: data + '&action=sendmail' //action defines which function to use in add_action
});

사용할 수 있습니다.php:

function send_my_mail(){
#do your stuff
}

add_action('wp_ajax_sendmail', 'send_my_mail');
add_action('wp_ajax_nopriv_sendmail', 'send_my_mail');

에 대해 읽어주세요.

레지스트리와 같은 시스템을 사용하여 모든 "글로벌" 값을 한 곳에 저장하는 것이 좋습니다.

레지스트리 설계 패턴

만약 이것이 당신에게 흥미가 있다면, 나의 작은 jQuery 플러그인이 있습니다.GitHub rep

<script type="text/javascript">
    $.Registry.set('urlMail', '<?php get_bloginfo('template_url'); ?>/templates/process.php';
</script>

레지스트리에서 값을 얻으려면 $를 사용해야 합니다.Registry.get('url Mail');

RRikesh에서 제공하는 코드를 사용하여 해결했지만, 교체했습니다.

data: data 

와 함께

data: data + '&action=sendmail'

언급URL : https://stackoverflow.com/questions/16274093/file-path-for-ajax-script-in-wordpress

반응형