Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax:发布动态Ajax内容_Javascript_Php_Jquery_Ajax_Fopen - Fatal编程技术网

Javascript Ajax:发布动态Ajax内容

Javascript Ajax:发布动态Ajax内容,javascript,php,jquery,ajax,fopen,Javascript,Php,Jquery,Ajax,Fopen,我正在尝试将div的内容写入一个新的html文件。div的内容是由ajax本身生成的,因此当我试图将内容发布到新文件时,写入该文件的只是原始html。是否有办法编写“ajaxed in”内容的div内容 这是我的ajax代码: $("#getSource").on('click', function(){ headerURL = $(".header-code").attr('data-url'); $.ajax({ url: headerURL, data: "fun

我正在尝试将
div
的内容写入一个新的html文件。
div
的内容是由ajax本身生成的,因此当我试图将内容发布到新文件时,写入该文件的只是原始html。是否有办法编写“ajaxed in”内容的
div
内容

这是我的ajax代码:

$("#getSource").on('click', function(){

  headerURL = $(".header-code").attr('data-url');
  $.ajax({
    url: headerURL,
    data: "function=showCode",
    success: function(data){
      $('code #mainCode').append(data);
    }
  });

  var bufferId =$("#mainCode").html();
  $.ajax({
     type : "POST",
     url : "postCode.php",
     data: {id : bufferId},
     dataType: "html",
     success: function(data){ 
       alert("ok");  
     }
  });
});
我的php代码:

$handle = fopen("test.html", 'w+');
$data = $_POST['id'];
if($handle) {
    if(!fwrite($handle, $data )) {
        echo "ok";
    }
}
test.html
中编写内容的最终结果:

<div id="mainCode"></div>

当我真的需要时:

<div id="mainCode">
  [dynamic content that is added by the user via ajax]
</div>

[用户通过ajax添加的动态内容]

在第一次调用附加内容后发出第二个AJAX调用。将JavaScritp代码更改为:

$("#getSource").on('click', function(){

  headerURL = $(".header-code").attr('data-url');
  $.ajax({
    url: headerURL,
    data: "function=showCode",
    success: function(data){
      $('code #mainCode').append(data);

      // second ajax call
      var bufferId =$("#mainCode").html();
      $.ajax({
         type : "POST",
         url : "postCode.php",
         data: {id : bufferId},
         dataType: "html",
         success: function(data){ 
           alert("ok");  
         }
      });
    }
  });
});

您还可以使用$.ajax返回的差异对象在第一个ajax请求之后运行第二个ajax请求:

headerURL = $(".header-code").attr('data-url');
$.ajax({
    url: headerURL,
    data: "function=showCode",
    success: function(data){
      $('code #mainCode').append(data);
    }
}).done(
  function() {
    var bufferId =$("#mainCode").html();
    $.ajax({
       type : "POST",
       url : "postCode.php",
       data: {id : bufferId},
       dataType: "html",
       success: function(data){ 
         alert("ok");  
       }
    }); 
  }
)

生成动态内容的代码在哪里?在代码运行之前,您是否设置了
bufferId
?Ajax调用是如何启动的?显示实际的代码。我的猜测是,您正在将其存储到变量中,并且从未使用新内容对其进行更新。我只是更新了上述代码。它非常简单,但原理是存在的。基本上,我点击一个按钮,它会从一个单独的php文件中以HTML格式转换成Ajax,但同时,我需要用正在转换的相同代码编写一个文件。我使用.when().then()来处理get和post轴。似乎有效。