【问题标题】:Run multiple ajax when page load页面加载时运行多个ajax
【发布时间】:2023-03-19 05:52:01
【问题描述】:

我写了三个如下的ajax函数:

function showClass()
{
    ...
    var url="getObjectProperty?"
    ....
}
function showDtPro()
{
    ...
    var url="getDataTypeProperty?"
    ....
}
function showObjPro()
{
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
     alert ("Your Browser Don't support AJAX");
     return;
  }
  var url="getObjectProperty?";
  url=url+"sid="+Math.random();
  xmlHttp.onreadystatechange=loadObjPro;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

除了“url”不同,其他都一样。这个ajax用于从三个不同的servlet获取数据并更新html中的标签。 下面是其中一个 servlet:

 //This function is used to get the data from an jena model and response the data to the client.
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("utf-8");
    //
    ArrayList<String> ObjectPropertyNames=new ArrayList<String>();
    //Define the jenaOwlModel 
    JenaOWLModel jenaOwlModel;
    try {
        OntModel ontmodel=MyModelFactory.getJenaModel().getOntModel();

        ExtendedIterator ObjectProperties = ontmodel.listObjectProperties();
        while(ObjectProperties.hasNext()){
            ObjectProperty property = (ObjectProperty) ObjectProperties.next();
            if(property.getLocalName()!=null)
            {
                ObjectPropertyNames.add(property.getLocalName());
            }
        }   
        } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }         
            response.setCharacterEncoding("utf-8");  
    PrintWriter out = response.getWriter(); 
            String AllObjectProperties="";
        for(int i=0;i<ObjectPropertyNames.size();i++)
        {                    AllObjectProperties=AllObjectProperties+ObjectPropertyNames.get(i)+"#";
        }
        System.out.print(AllObjectProperties);
        out.print(AllObjectProperties);  
        out.flush();  
        out.close();
}

另一个servlet和这个几乎一样。现在我想在页面加载时从三个servlet中加载数据。所以我在windows.onload函数中编写如下:

window.onload=function()
{
   showClass();
   showObjPro();
   showDtPro();
 }

但它并没有按预期运行。有时,只有最后一个函数showDtPro();fetch 数据。其他两个函数不获取数据。有时,标签应该由showClass() 更新,而showObjPro() 出现的数据来自showDtPro()

 <div id="class">
   <h4>Class</h4>
     <ul id="rdfclass" type="dclass"></ul> 
</div> 
<div id="obj">
     <h4>Object Property</h4>
     <ul id="rdfobjpro" type="dobjpro"></ul>
  </div>  
<div id="dt">
     <h4>Datatype Property</h4>
     <ul id="rdfdtpro" type="dtpro"></ul>  
</div> 

html标签在上面。相应地,这三个函数用于更新三个&lt;ul&gt;tag。 我是网络开发的初学者。我希望能得到你的帮助!谢谢!

【问题讨论】:

  • 你有很多重复的 JavaScript...
  • jQuery 标签的用途???
  • 据我所知没有。删除了标签。
  • 你是对的,我应该把重复的代码包装成函数。我会删除这个问题中不必要的代码

标签: javascript html ajax servlets


【解决方案1】:

那是因为 ajax 是异步的。您必须实现一些回调等。

在 ajax 的情况下执行不是同步的。

【讨论】:

  • 是的,但你能帮忙解释一下为什么它总是显示从最后一个函数获取的数据吗?以及为什么有时要通过 showClass() 更新标签,而 showObjPro() 出现从 showDtPro() 获取的数据。这三个函数是否相关?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多