我如何发送多个请求相同的文件,并获得所需的响应返回php使用jquery ajax


How do i send multiple requests for the same file and get the required response back in php using jquery ajax

var postID = $post->ID;
$.ajax({
             type: "POST",
             url: "<?php echo get_template_directory_uri();?>/b.php",
             data:{postID:postID},
             dataType: 'json',
             success: function(result){
                if(result!=''){
                    r = $.parseJSON(result);
                    final_rating = get_final_rating(r);
                    set_stars(final_rating);
                }
            }
        });
var arr = [a,b,c,d,e,f];
$.ajax({
                 type: "POST",
                 url: "<?php echo get_template_directory_uri();?>/b.php",
                 data:{star:arr, postID:postID},
                 async :false,
                 cache: false,
                 success: function(result){
                        if(result === '1')
                        {
                            final_rating = result;
                            set_stars(final_rating);
                        }
                    }
                });

你可以用jQuery做这样的事情:

var postID = <?php echo $post->ID; ?>,
    arr = [a,b,c,d,e,f],
    req1, req2;
req1 = $.ajax({
    type: "POST",
    url: "<?php echo get_template_directory_uri();?>/b.php",
    data: {postID:postID},
    dataType: 'json'
});
req2 = $.ajax({
    type: "POST",
    url: "<?php echo get_template_directory_uri();?>/b.php",
    data: {star:arr, postID:postID},
    async: false,
    cache: false
});
$.when(req1, req2).then(function (data1, data2) {
    // data1[0] = result
    if(data1[0] !== '') {
        r = $.parseJSON(result);
        final_rating = get_final_rating(r);
        set_stars(final_rating);
    }
    // data2[0] = result
    if(data2[0] === '1') {
        final_rating = result;
        set_stars(final_rating);
    }
});

var postID = $post->ID;应替换为:

var postID = <?php echo $post->ID; ?>;

你也做错了Ajax。您应该在admin-ajax.php上提出所有请求- http://codex.wordpress.org/AJAX_in_Plugins

然后使用不同的action参数来区分不同的Ajax调用。