As I learned so far, client-side AJAX script can read a static .json or .xml file from server e.g. http://www.mydomain.com/all_cities.xml.
据我所知,到目前为止,客户端AJAX脚本可以从服务器读取静态.json或.xml文件,例如http://www.mydomain.com/all_cities.xml。
But I need to generate a .json or .xml file dynamically according to the user input at client-side. e.g. imagine that the user selects a state from DropDownList1 and AJAX should fill DropDownList2 with that state's cities; while I don't like to send all cities and then filtering desired cities in client-side!
但我需要根据客户端的用户输入动态生成.json或.xml文件。例如想象一下,用户从DropDownList1中选择一个状态,而AJAX应该用该状态的城市填充DropDownList2;虽然我不喜欢发送所有城市,然后在客户端过滤所需的城市!
So, I would like to know how ASP.NET can handle this e.g. when it sees "http://www.mydomain.com/cities.json?state=9" then it should response with a dynamically created json file named cities.json which contains the cities which are in state #9?
所以,我想知道ASP.NET如何处理这个问题,例如当它看到“http://www.mydomain.com/cities.json?state=9”时,它应该响应动态创建的名为cities.json的json文件,其中包含状态#9的城市?
Thanks in advance!
提前致谢!
4 个解决方案
#1
1
There are a number of ways to do this - basically, the simplest is to have any standard web page (be it forms, an MVC controller/action/etc...) that acts like any normal webpage would with user input but instead of returning html, it returns XML. (By returns XML, I mean make the contents of the page solely XML and add the appropriate headers)
有很多方法可以做到这一点 - 基本上,最简单的是拥有任何标准网页(无论是形式,MVC控制器/动作/等......),就像任何普通网页一样,用户输入但不是返回html,它返回XML。 (通过返回XML,我的意思是使页面的内容仅为XML并添加适当的头文件)
Your question is too broad to give more details - but simply put, it's easily done and the client (JavaScript in this case) can't usually tell the difference.
你的问题太广泛了,无法提供更多细节 - 但简单地说,它很容易完成,客户端(在这种情况下是JavaScript)通常无法区分。
#2
0
If you want to create a WCF service, you could have a WebGet that accepts the state as a parameter and returns the cities for that state:
如果要创建WCF服务,可以使用WebGet接受状态作为参数并返回该状态的城市:
[WebGet(UriTemplate = "cities/{state}")]
public List<string> GetCities(string state)
{
List<string> citiesFromState;
// Build up a List of cities based on the state sent
return citiesFromState;
}
In the web.config for your application, set automaticFormatSelectionEnabled
to true so that WCF will look at the 'Accept' header of the HTTP request to determine what format to send the response in (e.g. xml or json):
在应用程序的web.config中,将automaticFormatSelectionEnabled设置为true,以便WCF查看HTTP请求的“Accept”标头,以确定发送响应的格式(例如xml或json):
<behavior name="webHttpBehavior">
<webHttp automaticFormatSelectionEnabled="true" helpEnabled="true"/>
</behavior>
This allows WCF to handle the serialization for you. Otherwise, you could do the serialization manually in the WebGet
.
这允许WCF为您处理序列化。否则,您可以在WebGet中手动执行序列化。
#3
0
If you want to read in static content, you'll only be able to use .XML by default since .JSON is not a default MIME-TYPE supported by IIS. Here is an example on how to read static a static XML file into your page using the ajax() method of jQuery.
如果您想要读取静态内容,则默认情况下您只能使用.XML,因为.JSON不是IIS支持的默认MIME-TYPE。下面是一个如何使用jQuery的ajax()方法将静态XML文件静态读入页面的示例。
Static File "cities.xml".
静态文件“cities.xml”。
<?xml version="1.0" encoding="utf-8" ?>
<cities>
<city>City A</city>
<city>City B</city>
<city>City C</city>
</cities>
jQuery Code to read Static XML and parse into a JavaScript array.
jQuery Code读取静态XML并解析为JavaScript数组。
$.ajax({
url: 'cities.xml',
dataType: 'xml',
success: function (xml)
{
var cities = [];
$(xml).find('city').each(function ()
{
cities[cities.length] = $(this).text();
});
// Do something with your array of values here.
}
});
#4
0
One of the way to achieve this is
实现这一目标的方法之一是
On Client Side: use jQuery and call a method/service to return JSON. Something like
在客户端:使用jQuery并调用方法/服务来返回JSON。就像是
function CallService() {
$.ajax({
type: "GET", //HTTP verb
url: serviceUrl,
data: "{}", //Data sent to server
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server e.g. json, xml etc.
success: OnSuccess
});
}
function OnSuccess(data) {
var options = $(".InputDropDown"); //.InputDropDown is class assigned to dropdown
//don't forget error handling!
$.each(data, function (index) {
var item = data[index];
options.append($("<option />").val(item.Id).text(item.Name));
});
}
this method will be called on first drop down change
首次下拉更改时将调用此方法
On Server Side: use either Web Service (or WCF) method or Page Methods (in your web app) that return JSON.
在服务器端:使用Web Service(或WCF)方法或返回JSON的Page Methods(在您的Web应用程序中)。
[WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Countries")]
List<Country> GetCountries();
If you are new to jQuery etc. I would strongly recommend you to go through this.
如果您是jQuery等新手,我强烈建议您仔细阅读。
An example of alternative approach (without WCF) of using PageMethod and plain JavaScript can be found at LINK-1 and LINK-2
在LINK-1和LINK-2上可以找到使用PageMethod和纯JavaScript的替代方法(没有WCF)的示例
#1
1
There are a number of ways to do this - basically, the simplest is to have any standard web page (be it forms, an MVC controller/action/etc...) that acts like any normal webpage would with user input but instead of returning html, it returns XML. (By returns XML, I mean make the contents of the page solely XML and add the appropriate headers)
有很多方法可以做到这一点 - 基本上,最简单的是拥有任何标准网页(无论是形式,MVC控制器/动作/等......),就像任何普通网页一样,用户输入但不是返回html,它返回XML。 (通过返回XML,我的意思是使页面的内容仅为XML并添加适当的头文件)
Your question is too broad to give more details - but simply put, it's easily done and the client (JavaScript in this case) can't usually tell the difference.
你的问题太广泛了,无法提供更多细节 - 但简单地说,它很容易完成,客户端(在这种情况下是JavaScript)通常无法区分。
#2
0
If you want to create a WCF service, you could have a WebGet that accepts the state as a parameter and returns the cities for that state:
如果要创建WCF服务,可以使用WebGet接受状态作为参数并返回该状态的城市:
[WebGet(UriTemplate = "cities/{state}")]
public List<string> GetCities(string state)
{
List<string> citiesFromState;
// Build up a List of cities based on the state sent
return citiesFromState;
}
In the web.config for your application, set automaticFormatSelectionEnabled
to true so that WCF will look at the 'Accept' header of the HTTP request to determine what format to send the response in (e.g. xml or json):
在应用程序的web.config中,将automaticFormatSelectionEnabled设置为true,以便WCF查看HTTP请求的“Accept”标头,以确定发送响应的格式(例如xml或json):
<behavior name="webHttpBehavior">
<webHttp automaticFormatSelectionEnabled="true" helpEnabled="true"/>
</behavior>
This allows WCF to handle the serialization for you. Otherwise, you could do the serialization manually in the WebGet
.
这允许WCF为您处理序列化。否则,您可以在WebGet中手动执行序列化。
#3
0
If you want to read in static content, you'll only be able to use .XML by default since .JSON is not a default MIME-TYPE supported by IIS. Here is an example on how to read static a static XML file into your page using the ajax() method of jQuery.
如果您想要读取静态内容,则默认情况下您只能使用.XML,因为.JSON不是IIS支持的默认MIME-TYPE。下面是一个如何使用jQuery的ajax()方法将静态XML文件静态读入页面的示例。
Static File "cities.xml".
静态文件“cities.xml”。
<?xml version="1.0" encoding="utf-8" ?>
<cities>
<city>City A</city>
<city>City B</city>
<city>City C</city>
</cities>
jQuery Code to read Static XML and parse into a JavaScript array.
jQuery Code读取静态XML并解析为JavaScript数组。
$.ajax({
url: 'cities.xml',
dataType: 'xml',
success: function (xml)
{
var cities = [];
$(xml).find('city').each(function ()
{
cities[cities.length] = $(this).text();
});
// Do something with your array of values here.
}
});
#4
0
One of the way to achieve this is
实现这一目标的方法之一是
On Client Side: use jQuery and call a method/service to return JSON. Something like
在客户端:使用jQuery并调用方法/服务来返回JSON。就像是
function CallService() {
$.ajax({
type: "GET", //HTTP verb
url: serviceUrl,
data: "{}", //Data sent to server
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server e.g. json, xml etc.
success: OnSuccess
});
}
function OnSuccess(data) {
var options = $(".InputDropDown"); //.InputDropDown is class assigned to dropdown
//don't forget error handling!
$.each(data, function (index) {
var item = data[index];
options.append($("<option />").val(item.Id).text(item.Name));
});
}
this method will be called on first drop down change
首次下拉更改时将调用此方法
On Server Side: use either Web Service (or WCF) method or Page Methods (in your web app) that return JSON.
在服务器端:使用Web Service(或WCF)方法或返回JSON的Page Methods(在您的Web应用程序中)。
[WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Countries")]
List<Country> GetCountries();
If you are new to jQuery etc. I would strongly recommend you to go through this.
如果您是jQuery等新手,我强烈建议您仔细阅读。
An example of alternative approach (without WCF) of using PageMethod and plain JavaScript can be found at LINK-1 and LINK-2
在LINK-1和LINK-2上可以找到使用PageMethod和纯JavaScript的替代方法(没有WCF)的示例