I wish to populate a JavaScript array with the results of a database query. From what I understand, a good approach is to populate a bunch of directories on my server with serialized XML or JSON files which my JS functions can then read into without having to access the database. Is this true? Should the database write these XML files upon user interaction, or should they be prepopulated?
我希望用数据库查询的结果填充JavaScript数组。根据我的理解,一个好的方法是使用序列化的XML或JSON文件在我的服务器上填充一堆目录,然后我的JS函数可以读取而无需访问数据库。这是真的?数据库是应该在用户交互时编写这些XML文件,还是应该预先填充?
2 个解决方案
#1
1
From the details you have provided:
根据您提供的详细信息:
I Personally would create a Web Service endpoint returning your linq query serialized to JSON using JSON.NET or an equivalent. The endpoint can then be called using ajax within your client page.
我个人会创建一个Web服务端点,使用JSON.NET或等效的方法将您的linq查询序列化为JSON。然后可以使用客户端页面中的ajax调用端点。
You can check this example out on ASP.NET for how to create a asmx (legacy) web service. Also you can look at this example of using the [WebMethod] attribute .
您可以在ASP.NET上查看此示例,了解如何创建asmx(旧版)Web服务。您也可以查看使用[WebMethod]属性的示例。
A web method in your code behind files.
代码隐藏文件中的Web方法。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetMyQueryResultsAsJson()
{
var results = GetQueryResults();
var jss = new JavaScriptSerializer();
string json = jss.Serialize(results);
return json;
}
A ajax call on your client page:
在您的客户端页面上调用ajax:
function loadData() {
var myArray = [];
$.getJSON("<%= ResolveUrl("~/MyPage.aspx/GetMyQueryResultsAsJson") %>", function (data) {
$.each(data, function(obj) {
myArray.push([obj.id, obj.someValue, obj.AnotherValue]);
});
});
}
#2
1
I used jQuery ajax
with generic http handler
returing json
I created handler and put my business logic there.
It return me the result in form of json
I iterated the json
using jquery.
And created my html form that.
我使用jQuery ajax与通用的http处理程序returing json我创建了处理程序并将我的业务逻辑放在那里。它以json的形式返回结果我使用jquery迭代json。并创建了我的html表单。
Edit 1
Here are some useful links
http://www.codeproject.com/Articles/203621/Call-HTTPhandler-from-jQuery-Pass-data-and-retriev
http://www.sharepointnutsandbolts.com/2010/11/sp2010-ajax-part-4-returning-json-from.html
以下是一些有用的链接http://www.codeproject.com/Articles/203621/Call-HTTPhandler-from-jQuery-Pass-data-and-retriev http://www.sharepointnutsandbolts.com/2010/11/sp2010- Ajax的部分-4-返回-JSON-from.html
Edit 2
You can also take benefit of jtemplate
http://www.joe-stevens.com/2010/01/05/using-the-jtemplate-jquery-plugin-with-ajax-and-asp-net/
http://encosia.com/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/
你也可以利用jtemplate http://www.joe-stevens.com/2010/01/05/using-the-jtemplate-jquery-plugin-with-ajax-and-asp-net/ http:// encosia的.com /使用-jquery的和 - ASPNET-AJAX到集结一个客户端侧中继器/
#1
1
From the details you have provided:
根据您提供的详细信息:
I Personally would create a Web Service endpoint returning your linq query serialized to JSON using JSON.NET or an equivalent. The endpoint can then be called using ajax within your client page.
我个人会创建一个Web服务端点,使用JSON.NET或等效的方法将您的linq查询序列化为JSON。然后可以使用客户端页面中的ajax调用端点。
You can check this example out on ASP.NET for how to create a asmx (legacy) web service. Also you can look at this example of using the [WebMethod] attribute .
您可以在ASP.NET上查看此示例,了解如何创建asmx(旧版)Web服务。您也可以查看使用[WebMethod]属性的示例。
A web method in your code behind files.
代码隐藏文件中的Web方法。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetMyQueryResultsAsJson()
{
var results = GetQueryResults();
var jss = new JavaScriptSerializer();
string json = jss.Serialize(results);
return json;
}
A ajax call on your client page:
在您的客户端页面上调用ajax:
function loadData() {
var myArray = [];
$.getJSON("<%= ResolveUrl("~/MyPage.aspx/GetMyQueryResultsAsJson") %>", function (data) {
$.each(data, function(obj) {
myArray.push([obj.id, obj.someValue, obj.AnotherValue]);
});
});
}
#2
1
I used jQuery ajax
with generic http handler
returing json
I created handler and put my business logic there.
It return me the result in form of json
I iterated the json
using jquery.
And created my html form that.
我使用jQuery ajax与通用的http处理程序returing json我创建了处理程序并将我的业务逻辑放在那里。它以json的形式返回结果我使用jquery迭代json。并创建了我的html表单。
Edit 1
Here are some useful links
http://www.codeproject.com/Articles/203621/Call-HTTPhandler-from-jQuery-Pass-data-and-retriev
http://www.sharepointnutsandbolts.com/2010/11/sp2010-ajax-part-4-returning-json-from.html
以下是一些有用的链接http://www.codeproject.com/Articles/203621/Call-HTTPhandler-from-jQuery-Pass-data-and-retriev http://www.sharepointnutsandbolts.com/2010/11/sp2010- Ajax的部分-4-返回-JSON-from.html
Edit 2
You can also take benefit of jtemplate
http://www.joe-stevens.com/2010/01/05/using-the-jtemplate-jquery-plugin-with-ajax-and-asp-net/
http://encosia.com/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/
你也可以利用jtemplate http://www.joe-stevens.com/2010/01/05/using-the-jtemplate-jquery-plugin-with-ajax-and-asp-net/ http:// encosia的.com /使用-jquery的和 - ASPNET-AJAX到集结一个客户端侧中继器/