简体   繁体   English

在页面加载时使用jquery读取xml

[英]on load of a page read an xml using jquery

From 1st page I pass 2 parameters http://in164263:5050/csm/csminfo.jsp?cfgid=48&filepath=files/csmclientbuckeye.xml 从第一页开始,我传递2个参数http://in164263:5050/csm/csminfo.jsp?cfgid=48&filepath=files/csmclientbuckeye.xml

How to read the request parameter in csminfo.jsp and get xml file from filepath ? 如何读取csminfo.jsp的request参数并从文件filepath获取xml文件?

I suppose for reading and parsing xml I can us something like this 我想读取和解析xml我可以像这样

$.ajax({
    type: "GET",
    url: "sites.xml", //here i want to read request parameter `filepath`
    dataType: "xml",
    success: function(xml) {

    }
});

The problem is i am passing the filepath on onLoad of page's body, so the spry region tries to load itself through the dataset(which is not yet initialised) but since it does not have the path yet it is not displayed. 问题是我在页面正文的onLoad上传递文件filepath ,因此spry区域尝试通过数据集(尚未初始化)加载自身,但由于它没有路径,因此未显示。 So how can I get the filepath before onLoad so that spry:region gets this filepath? 那么,如何在onLoad之前获取文件filepath ,以便spry:region获得此文件路径?

You can use jQuery.ajax() with dataType set to XML if the server, from which you are fetching data doesn't send proper content-type. 如果从中获取数据的服务器未发送正确的内容类型,则可以使用data.type设置为XML的jQuery.ajax()。 Then in success callback first argument is your XML. 然后,回调成功的第一个参数是您的XML。

 $.ajax('path/xml', {
   success: function(xml) {
     var $xml = $(xml);
     alert($xml.find( "title" ));
   }
 });

Or use jQuery.parseXML inside success callback. 或在成功回调中使用jQuery.parseXML

With JSP you can use something like this to setup properly your ajax call 使用JSP,您可以使用类似这样的方法来正确设置ajax调用

$.ajax({
    type: "GET",
    url: "<%= request.getParameter("filepath") %>",
    dataType: "xml",
    success: function(xml) {

    }
});

Otherwise, if you really NEED to read the get or post parameters via javascript (jQuery), you can follow this answer : how to get GET and POST variables with JQuery? 否则,如果您确实需要通过javascript(jQuery)读取get或post参数,则可以遵循以下答案: 如何使用JQuery获取GET和POST变量?

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

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