简体   繁体   English

通过AJAX在div内加载HTML

[英]Load HTML inside a div via AJAX

i have bone.php and forum.php 我有bone.phpforum.php

i want to send data to forum.php from bone.php when i click #result via AJAX 当我通过AJAX单击#result时,我想将数据从bone.php发送到forum.php

forum.php gets data and does its job inside server and creates an HTML page forum.php获取数据并在服务器内完成其工作,并创建一个HTML页面

i want this HTML page inside bone.php #result . 我想要这个HTML页面在bone.php #result

Is this possible? 这可能吗?

i wrote codes like that. 我写了这样的代码。

$.ajax({

url: "forum.php",
data: 'webpage_id='+webpage_id ,
success: function( ) {
    $('#result').load("forum.php"); 
},
error: function( xhr, status, errorThrown ) {
  alert( "Sorry, there was a problem!" );
  console.log( "Error: " + errorThrown );
  console.log( "Status: " + status );
  console.dir( xhr );
},

})

I am getting this messsage Undefined index: webpage_id in \\forum.php on line 15 . Undefined index: webpage_id in \\forum.php on line 15得到此消息Undefined index: webpage_id in \\forum.php on line 15 However i am sending data. 但是我正在发送数据。 What could be the problem? 可能是什么问题呢?

What is more interesting is it gives no error and loads the forum.php correctly. 更有趣的是它没有错误,并且forum.php正确加载forum.php It just doesn't see the data. 它只是看不到数据。

To start with, 首先,

$('#result').load("forum.php");

this is an ajax call by itself. 这本身就是一个ajax调用。

 $.ajax({

url: "forum.php",
success: function( data ) {
    $('#result').html(data); 
});

Equilevant to this. 与此相关。

you're performing the request twice, when you do $('#result').load("forum.php"); 当您执行$('#result').load("forum.php");时,您将执行两次请求 in the success block, you're actually running the request a second time. 在成功块中,您实际上是在第二次运行请求。 You can do this: 你可以这样做:

$('#result').load("forum.php?webpage_id=" + webpage_id); 

Or you can do this: 或者您可以这样做:

$.ajax({

url: "forum.php",
data: 'webpage_id='+webpage_id ,
success: function(data) {
    $('#result').html(data); 
},
error: function( xhr, status, errorThrown ) {
  alert( "Sorry, there was a problem!" );
  console.log( "Error: " + errorThrown );
  console.log( "Status: " + status );
  console.dir( xhr );
},

})

Try 尝试

$.ajax({
   url: "forum.php",
   data: 'webpage_id='+webpage_id ,
   success: function(response) {
      $('#result').html(response);
   },
   error: function( xhr, status, errorThrown ) {
      alert( "Sorry, there was a problem!" );
      console.log( "Error: " + errorThrown );
      console.log( "Status: " + status );
      console.dir( xhr );
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM