AJAX调用outerHTML uncaught typeError

时间:2015-07-28 12:07:45

标签: javascript jquery html

在CMS环境中实现此代码时。得到beloe错误。

未捕获的TypeError:无法读取属性' outerHTML'未定义的

有人可以帮我解决这个问题吗?

提前致谢

main.html中:

<div class="pwrapper">
                  <select name="country" id="scountry">
                    <option id="EN" value="global.html">GLOBAL, ENGLISH</option>
                    <option id="AU" value="australia.html">AUSTRALIA</option>
                  </select>
              </div>
              <script type="text/javascript">
                  $(document).ready(function ()  {                       
                      $('.country #scountry').on('change', function(e){
                          var ajaxURL = $(this).val();
                          $.ajax({
                              url: ajaxURL,
                              success: function(data) {
                                  var htmlData = $(data);
                                   $('#scontainer').html(htmlData.filter('#ajax-container').get(0).outerHTML);
                              }
                          });                          
                      });                          
                      $('.countrySelector #scountry').trigger('change');
                  });
              </script>
</div>

<div class="wrapper">
 <section class="no-padding">
     <div id="scontainer">
     </div>
 </section>
</div>

global.html(在main.html下作为子页面):

<div id="ajax-container">
   Hello World!!
</div>

2 个答案:

答案 0 :(得分:0)

替换此行:

htmlData.filter('#ajax-container').get(0).outerHTML

使用此代码

htmlData.find('#ajax-container').html();

答案 1 :(得分:0)

这是使用load()的理想案例,$.ajax$.ajax的快捷方式。

您可以使用以下命令替换当前的$('#scontainer').load(ajaxURL +' #ajax-container); 请求和回调:

DataTable tbl = new DataTable();
using(OleDbConnection c = new OleDbConnection())
using (OleDbCommand cmd = new OleDbCommand("SELECT * from omed", c))
{
  c.ConnectionString = "Provider=Microsoft.Ace.Oledb.12.0;Data Source=" + 
     Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Database.accdb";

  c.Open();
  OleDbDataReader reader = cmd.ExecuteReader();
  tbl.Load(reader);
  c.Close();
}

foreach (DataRow r in tbl.Rows)
{
    DataGridViewRow row = new DataGridViewRow();
    row.CreateCells(dataGridView1);  // this line was missing
    row.Cells[0].Value = r["ID"].ToString();
    row.Cells[1].Value = r["CELL"].ToString();
    row.Cells[2].Value = r["ncc"].ToString();
    row.Cells[3].Value = r["bcchno"].ToString();
    dataGridView1.Rows.Add(row);
    for (int i = 0; i < 31; i++)
    {
        var v = r["n_cell_" + i].ToString();
        row.Cells[i * 3 + 4].Value = v; 
        var resultRow = tbl.AsEnumerable().Last (t => t.Field<string>("CELL").Contains(v));
        row.Cells[i * 3 + 5].Value = resultRow["ncc"].ToString();
        row.Cells[i * 3 + 6].Value = resultRow["bcchno"].ToString();
    }
}

url后面的空格分隔选择器用于确定要在容器中插入的请求的html响应的哪些片段。如果返回的唯一根元素是您想要的ID,则不需要对片段进行任何过滤,并且可以删除片段选择器

参考:load() docs