1

1つのファイル(test_ajax.php)に、変更時にjQuery Ajax()を介して短いメッセージを含む別のページ(registration_form_race_type.php)をロードするファイルがあります。「test_ajax.php」が絶対URL(次のURL)を介してアクセスされる場合は正常に機能します。

http://46.20.119.207/~asuntosf/wordpress_test/wp-content/themes/test_ajax/test_ajax.php

しかし、驚くべきことに、まったく同じページ「test_ajax.php」がWordPressアドレス(次のアドレス)を介してアクセスされると、Ajax機能は機能しなくなります。

http://46.20.119.207/~asuntosf/wordpress_test/?page_id=13

私はこれらの両方のURLが同じ2つのPHPファイルを指していると主張します。

これが「test_ajax.php」のコードです:

<?php
/*
Template Name: Page Test Ajax 01
*/
?>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
    <script type="text/javascript">
        jQuery(function () {    
            jQuery('#event_id_from_list').change(function() {       
                var event = jQuery("#event_id_from_list").val(); 
                var data = "event_id=" + event;         
                jQuery.ajax({
                    url: 'registration_form_race_type.php', 
                    type: 'GET',
                    data: data,
                    success: function(data){ 
                        jQuery('#div_race_type').html(data); 
                    }
                });         
            });
        });
    </script>
</head>
<body>
    <select class='required' type="text" name="event_id_from_list" id="event_id_from_list" />
        <option value='Paris'>Paris</option>
        <option value='London'>London</option>
        <option value='Rome'>Rome</option>
    </select>   
    <div id='div_race_type' class='section'>            
        <?php require_once('registration_form_race_type.php'); ?>           
    </div>
</body>
<html>

そして、Ajax経由で呼び出されたページのコード「registration_form_race_type.php」:

<?php if (isset($_GET['event_id'])) echo 'you selected '.$_GET['event_id']; ?>
4

1 に答える 1

1

この振る舞いについては何も奇妙なことはありません。registration_form_race_type.phpjQueryで参照しているだけregistration_form_race_type.phpで、現在のディレクトリで検索するという、要求されていることを実行します。registration_form_race_type.phpに住んでいて、中http://46.20.119.207/~asuntosf/wordpress_test/wp-content/themes/test_ajax/には住んでいませんhttp://46.20.119.207/~asuntosf/wordpress_test/

registration_form_race_type.phpからアクセスする場合はhttp://46.20.119.207/~asuntosf/wordpress_test/?page_id=13、コードを次のように変更する必要があります。

jQuery.ajax({
    url : 'wp-content/themes/test_ajax/registration_form_race_type.php',
    type : 'GET',
    data : data,
    success : function (data) {
        jQuery('#div_race_type').html(data);
    }
});
于 2013-03-15T06:23:12.723 に答える