代码之家  ›  专栏  ›  技术社区  ›  Jimmy

将变量从一个ajax调用发送到下一个ajax调用

  •  0
  • Jimmy  · 技术社区  · 7 年前

    这就是我所拥有的,可以完美地工作并返回reportID:

    $(document).ready(function(){
        $("#r2").click(function(){
            $.ajax({
            url: "report.php", 
            dataType: 'json',
            data: { 
                type: 'queue', 
                ref: 2
            },
            success: function(result){
                console.log(result.reportID);
            }});
        });
    });
    

    它返回:

    {"reportID":1876222901}
    

    ref: 1876222901

    $(document).ready(function(){
        $("#r2").click(function(){
            $('#loading').show();
            $.ajax({
            url: "report.php", 
            dataType: 'json',
            data: { 
                type: 'queue', 
                ref: 2
            },
            success: function(result){
                console.log(result);
                $.ajax({
                url: "report.php", 
                dataType: 'json',
                data: { 
                type: 'get', 
                ref: result.reportID
                },
                success: function(result){
                    console.log(result);
                }
                });
            }});
        });
    });
    

    我所坚持的是将变量从第一个ajax调用的结果传递到第二个ajax调用的方式。好像到不了那里。如何发送报告ID 1876222901 我的第二个电话?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Benny Powers    7 年前

    不需要jQuery,只需使用浏览器内置的domapi和fetchapi就可以做到这一点

    const r2 = document.getElementById('r2')
    const loading = document.getElementById('loading')
    
    const handleAsJson = response => response.json()
    
    const fetchRef = ({reportId}) =>
      fetch(`report.php?type=get&ref=${reportId}`)
    
    document.onready = () => {
      r2.addEventListener('click', () => {
        loading.show();
        // you don't have to interpolate here, but in case these
        // values are variable...
        fetch(`report.php?type=${'queue'}&ref=${2}`)
          .then(handleAsJson)
          .then(fetchRef)
          .then(handleAsJson)
          .then(console.log)
      })
    }
    
        2
  •  1
  •   You Nguyen    7 年前

    解决办法是不写东西

    ref: result.reportID

    你必须这样写

    ref: result.reportID.reportID

    因为正如你所说,你第一次使用 console.log(result.reportID) 结果是 {"reportID":1876222901} . 这意味着你必须链接两次点符号才能得到这个值 1876222901

    需要说明的是:

    $(document).ready(function(){
        $("#r2").click(function(){
            $('#loading').show();
            $.ajax({
            url: "report.php", 
            dataType: 'json',
            data: { 
                type: 'queue', 
                ref: 2
            },
            success: function(result){
                console.log(result); // as you said, console.log(result.reportID) return {"reportID":1876222901}
                $.ajax({
                  url: "report.php", 
                  dataType: 'json',
                  data: { 
                  type: 'get', 
                  ref: result.reportID //which means, this line would be => ref: {"reportID":1876222901}
                  // we will correct it like this => ref: result.reportID.reportID
                  // then we properly get => ref:1876222901
                },
                success: function(result){ 
                    console.log(result); 
                }
                });
            }});
        });
    });

    希望它能纠正你的错误。

        3
  •  0
  •   Rahul Dudharejiya    7 年前

    我认为您可以尝试在第一次使用ajax之后创建另一个调用函数

    $(document).ready(function(){
        $("#r2").click(function(){
            $.ajax({
            url: "report.php", 
            dataType: 'json',
            data: { 
                type: 'queue', 
                ref: 2
            },
            success: function(result){
                console.log(result.reportID);
                callSecondAjax(result.reportID)
            }});
        });
    });
    

    现在让这个函数

    function callSecondAjax(reportId){
    // do some stuff 
    }