如何利用另一个 .AJAX 函数的响应值作为参数插入到 POST 请求中

How to utilize a value from the response of one .AJAX function in another as a parameter to insert into the POST request

我正在尝试将从一个 ajax 函数中的响应接收到的值用作 html 文件中另一个 ajax 函数调用的参数。 如果这是非常基础的,我很抱歉,但我对此很陌生。

我尝试调用该值,就像我在 HTML 和许多其他尝试的正文中所做的一样。

1 - 这是初始调用(工作):

$.ajax({
          "url":api_base+"/endpoint_01",
          "type":"GET",
          "contentType":"application/json",
          "success":function(data){
            var data = data[0];
            $('#dash_value').html(data.value);
          }
        });

2 - 这是我在 html 中访问它的方式 (:

<div class="highlighted-text" id="dash_value"></div>

3 - 我不知道以后如何在文件中使用它:

function function_02(){
          $.ajax({
            "url":api_base+"endpoint_02?value=" + dash_value,
            "type":"POST",
            "beforeSend":function(){
              $('#button_01').prop('disabled', true);
            },
            "dataType":"text",
            "success":function(data){
              swal(data, {
                icon : "success",
                buttons: false,
                timer: 3000
              });
            },
            "complete":function(){
              $('#button_01').prop('disabled', false);
            }
          });
        }

POST应该这样发送:

   https://endpoint02?value=dash_value

但除此之外我什么都得到了。请帮忙。

尝试在 Ajax 方法范围之外声明一个变量,以便方法外部的代码稍后可以访问它,然后将该变量分配给 ajax 范围内的返回数据:

var outerScopeData = "";

$.ajax({
  "url":api_base+"/endpoint_01",
  "type":"GET",
  "contentType":"application/json",
  "success":function(data){
    var data = data[0];

    // add this here:
    outerScopeData = data.value;

    $('#dash_value').html(data.value);
  }
});

您需要在进行第二个 ajax 调用之前定义 dash_value



function function_02(){
         const dash_value = $("dash_value").data()
          $.ajax({
            "url":api_base+"endpoint_02?value=" + dash_value,
            "type":"POST",
            "beforeSend":function(){
              $('#button_01').prop('disabled', true);
            },
            "dataType":"text",
            "success":function(data){
              swal(data, {
                icon : "success",
                buttons: false,
                timer: 3000
              });
            },
            "complete":function(){
              $('#button_01').prop('disabled', false);
            }
          });
        }