weixin_33711641 2014-04-14 14:36 采纳率: 0%
浏览 17

jQuery-无法接收数据

I have a problem, where I am using jQuery/Ajax to run a form, send the data to an external file, and then update a part of my page. But I ran into a problem:

The external site works. It prints out this div with the id="content" when I run the site manually.

But when im trying to receive this data from another document through jQuery, it will not return this field.

It runs all the other code on the external file just fine, as it executes the database calls in the external file just fine. But there is no data returned, as when the code is executed, the text of the "testlog" div (text here) just disappears, but isn't replaced.

So can you help me with why I can't retrieve the data from the external file?

First, here is the relevant part of the file I want to import into:

<script>
$(document).ready(function() {
 $( "#moveN" ).submit(function( event ) {
   // Stop form from submitting normally
   event.preventDefault();
   var $form = $( this );
   term = $form.find( "input[name='direction']" ).val();
   var posting = $.post( "/modules/world/movement/move.php", { direction: term } );
   posting.done(function( data ) {
      var content = $("#content", data).html();
      $( "#testlog" ).html( content );
   });
  });
});
</script>
<div id="testlog">Text here<div>

And here the relevant part of the external file:

<div id="content">
Part i want imported. This should be filled in with data from this external file, but  now we are just trying to get the retrieved by the other file.
</div>

I hope some of you can help a novice out :)

  • 写回答

3条回答 默认 最新

  • 叼花硬汉 2014-04-14 14:39
    关注

    you could use jQuery.get() to get the html from the html form page

    $.get( "ajax/form-page.html", function( data ) {
      //here you have acess to the html form the form page
      $( ".result" ).html( data );
      alert( "Load was performed." );
    });
    

    jQuery.get() is a shorthand Ajax function, which is equivalent to:

    $.ajax({
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });
    

    Another option is use jquery´s load function

        <!DOCTYPE html>
        <html>
        <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
        </script>
        <script>
        $(document).ready(function(){
          $("button").click(function(){
            $("#div2").load("form.html");
          });
        });
        </script>
        </head>
        <body>
    
        <div id="div1"><h2>jQuery AJAX Load</h2></div>
        <div id="div2"><h2>Let jQuery AJAX Change This Text</h2></div>
        <button>Get External Content</button>
    
        </body>
        </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch