首页 > 解决方案 > [JS][AJAX] 第二个 ajax 不起作用。为什么?

问题描述

我有一点问题 :) 第一个 ajax 返回正确的值。然后我从第一个答案中提取文本,我想将此文本发送到另一个 ajax 文件。但第二个发送一个空值。为什么?所以这是我的代码:

var fileName = "record.wav";
var formdata = new FormData();
formdata.append('file', blob);
$.ajax({
  url: "sendFileAudio.php",
  type: 'POST',
  data: formdata,
  // async: true,
  cache: false,
  contentType: false,
  processData: false,
  beforeSend: function() {
    $("#loaderStt").show();
  },
  success: function(data) {
    console.log(data);
    var messagesContainerOther = $(".messages");
    messagesContainerOther.append(
      data
    );
    var audioRsp = $(data).text();
    var pytanie = audioRsp.valueOf();
    // alert(typeof audioRsp);
    document.getElementById("wiadomosc").innerHTML = pytanie;
    // var pytanie = $("#wiadomosc").text();     
    $.ajax({
      url: "sendMessageSamara.php",
      type: 'POST',
      data: pytanie,
      beforeSend: function() {
        $("#loader").show();
      },
      success: function(data) {
        // console.log(data);                                         
        // alert("2"+ref);                                         
        console.log("poszło");
        console.log(pytanie);
        var messagesContainerOther = $(".messages");
        messagesContainerOther.append(
          data
        );
        messagesContainerOther.finish().animate({
            scrollTop: messagesContainerOther.prop("scrollHeight")
          },
          250
        );
      },
      complete: function(data) {
        $("#loader").hide();
      },
    });
  },
  complete: function(data) {
    $("#loaderStt").hide();
  },
});

标签: javascriptjqueryajax

解决方案


您没有在第二个请求中发送键/值对,仅发送一个值

改变

data: pytanie

data: {somePropertyName: pytanie}

并在 php 中接收:

$_POST['somePropertyName'];

推荐阅读