在ASP.NET MVC 4网站的客户端读取XML

时间:2021-11-03 15:57:55

I am working on a ASP.NET website and I need to get XML from an external url. It works great using ASP.NET MVC's XmlDocument but now I need to parse the XML without going to the server again, so client-side. An example of what I want to do:

我正在ASP.NET网站上工作,我需要从外部URL获取XML。它使用ASP.NET MVC的XmlDocument很好用,但现在我需要解析XML而无需再次访问服务器,所以客户端。我想做的一个例子:

Presume we have 3 <select> dropdownlists, the first one contains all carmakes, the second one contains all car models and the third one all types. So, when the page loads, we have the first dropdown with all carmakes and the other two are disabled. When I select a make, I want to load all models of the selected make (dropdown nr. 3 still disabled). Then when I select a car model, I want to load all types of the selected model.

假设我们有3个

All makes can be loaded by ASP.NET's XmlDocument which works fine already, but the other data needs to be parsed client-side. I tried this using jQuery:

所有品牌都可以通过ASP.NET的XmlDocument加载,它已经正常工作,但其他数据需要在客户端解析。我尝试使用jQuery:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $.ajax({
                    type: "GET",
                    url: "-- hidden --",
                    dataType: "xml",
                    success: parseXml
                });

                function parseXml(xml){
                    $(xml).find("Make").each(function(){
                        $("#output").append($(this).find("carmake").text() + "<br />");
                    });
                }
            });
        </script>
    </head>
    <body>
        <div id="output"></div>
    </body>
</html>

But that doesn't work. I read a lot of questions where the sulution was to use crossDomain: true and dataType: jsonp but that doesn't work either. I also read the server I'm getting the XML from needs to support jsonp, but there is no way I can do something about that, because it is not my server but a webservice.

但这不起作用。我读了很多问题,其中使用crossDomain:true和dataType:jsonp,但这也不起作用。我还阅读了服务器,我从需要支持jsonp获取XML,但我无法做到这一点,因为它不是我的服务器而是web服务。

As you may have already seen, the sample-url to get all makes is -- hidden --/make.alx. Then when the user selected a make, I can get the models of the make by sending the carcode in the url, for example: -- hidden --/model.alx?carcode=2

正如您可能已经看到的那样,获取所有品牌的sample-url是 - hidden - / make.alx。然后,当用户选择了一个make时,我可以通过在url中发送carcode来获取make的模型,例如: - hidden - / model.alx?carcode = 2

So in short the question is: How can I get this working with jQuery if possible and if not, how can I get this working another way? I there a nice way to do this using ASP.NET MVC but without having the whole page reloaded?

所以简而言之,问题是:如果可能的话,如何使用jQuery,如果没有,我怎么能以另一种方式工作呢?我有一个很好的方法来使用ASP.NET MVC,但没有重新加载整个页面?

This website is a nice example of what I want. As you can see here just under the header you can select a car make, model and type.

这个网站是我想要的一个很好的例子。正如您可以在标题下看到的那样,您可以选择汽车品牌,型号和类型。

Thanks in advance!

提前致谢!

2 个解决方案

#1


1  

Not possible through jQuery, but you can create a proxy page for this in your website.

不可能通过jQuery,但您可以在您的网站中为此创建代理页面。

#2


0  

Ok, for those who are struggling on this too, here is how I solved this, thanks to @GovindKamalaPrakashMalviya. He pushed me in the right direction, I searched a while and finally found out it has to be done with JSON searched how to do it and finally got it working using this website. Works great! thanks Govind!

好吧,对于那些正在努力解决这个问题的人来说,感谢@GovindKamalaPrakashMalviya,这就是我解决这个问题的方法。他把我推向了正确的方向,我搜索了一会儿,终于发现必须要用JSON搜索如何做到这一点,最后让它使用这个网站。效果很好!谢谢Govind!

#1


1  

Not possible through jQuery, but you can create a proxy page for this in your website.

不可能通过jQuery,但您可以在您的网站中为此创建代理页面。

#2


0  

Ok, for those who are struggling on this too, here is how I solved this, thanks to @GovindKamalaPrakashMalviya. He pushed me in the right direction, I searched a while and finally found out it has to be done with JSON searched how to do it and finally got it working using this website. Works great! thanks Govind!

好吧,对于那些正在努力解决这个问题的人来说,感谢@GovindKamalaPrakashMalviya,这就是我解决这个问题的方法。他把我推向了正确的方向,我搜索了一会儿,终于发现必须要用JSON搜索如何做到这一点,最后让它使用这个网站。效果很好!谢谢Govind!