简体   繁体   English

我如何使用jquery ajax在同一个文件中发送多个请求并在php中获取所需的响应

[英]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);
                        }
                    }
                });

You can do something like this with jQuery: 您可以使用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; should be replaced by: 应替换为:

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

You're also doing Ajax wrong. 您也做错了Ajax。 You should make all requests on admin-ajax.php - http://codex.wordpress.org/AJAX_in_Plugins 您应该在admin-ajax.php - http: admin-ajax.php上发出所有请求

You then use different action parameters to differentiate between different Ajax calls. 然后,您可以使用不同的action参数来区分不同的Ajax调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PHP和jQuery发送安全的AJAX请求 - How to send secure AJAX requests with PHP and jQuery 如何在同一页面上从Django和Ajax获得多个发帖请求? - How do I get multiple post requests from Django and Ajax on the same page working? 如何使用ajax和php发送多个同名输入数据 - How to send data multiple same name input using ajax and php 如何在jquery中创建一个关联数组并通过ajax发送它来解析php? - How do I create an associate array in jquery and send it via ajax to get parsed with php? AJAX / Jquery - 从php文件获取响应 - AJAX/Jquery - Get response from php file 如何从多个下拉选择框中获取数据以发送jquery ajax调用? - How do I get data from Multiple dropdown select boxes to send a jquery ajax call? 如何获取多个复选框值并通过jquery中的ajax发送值? - How do I get multiple checkbox values and send the values via ajax in jquery? 我需要发送多个 ajax 请求并使用 jQuery 比较数据。 此代码有效,但速度太慢。 如何优化它? - I need to send multiple ajax requests and compare data using jQuery. This code works, but too slow. How to optimize it? 带有响应array []的jQuery Ajax PHP POST方法instad发送了数据。 如何获得发送值作为响应? - jQuery Ajax PHP POST method with response array[] instad sent data. How to get send values in response? 当我发送多个ajax请求时,数据相同 - Same data when i send multiple ajax requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM