简体   繁体   English

如何在JavaScript中从浏览器加载xml数据

[英]How to load xml data from browser in javascript

I am trying to load data from browser using URL. 我正在尝试使用URL从浏览器加载数据。 Right now I am using javascript for this. 现在,我正在为此使用JavaScript。

window.onload = function()
        {
            // this is URL from which i want to load data. 
            // myURL in this xml file is there. (myURL is running in localhost)
            var url = "myURL&callback=processDATA"; 
            loadDATA(url);
        }

function loadDATA(url)
        {

            var headId = document.getElementsByTagName('head')[0];
            var newScript = document.createElement('script');
            newScript.type = 'text/javascript';
            newScript.src = url;
            headId.appendChild(newScript);
        }

function processDATA(feed) //this is function that is called after loadDATA(url).
        {
             // i want my XML file in feed variable. 
             // But this function is not called after  loadDATA.
        }

i do not know what to do. 我不知道该怎么办。 Please help me. 请帮我。

This function implies that the server API knows to wrap your function with your "callback" parameter for JSONP. 此函数意味着服务器API知道使用JSONP的“回调”参数包装您的函数。 Are you creating the server API which is returning the XML as well? 您是否正在创建同时返回XML的服务器API? If so you'll need to have it check for the existence of a callback querystring parameter which it would then use to return the data. 如果是这样,则需要让它检查是否存在回调querystring参数,然后将其用于返回数据。 This is how it would look in c#: 这就是在C#中的样子:

if (request.QueryString["callback"] != null)
    response.write(request.QueryString["callback"] + "('" + xmldata + "');");

So it executes on return. 因此它在返回时执行。

If this is a public API find out if they have a specific callback paramater name for jsonp. 如果这是公共API,请找出它们是否具有jsonp的特定回调参数名称。 It is commonly called jsoncallback. 通常称为jsoncallback。

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

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