创建JSON回报和QUOT;串"从web服务与jQuery的AJAX应用 [英] Creating JSON return "strings" from a webservice for use with jquery ajax
问题描述
我想实现一个简单的Web服务将使用教程一个asp.net应用程序在这里找到:的http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx#1301和 http://dotnetslackers.com/articles/ajax/Using- jQuery的-与-ASP-NET.aspx
I tried implementing a simple web service into an asp.net application using the tutorial found here: http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx#1301 and http://dotnetslackers.com/articles/ajax/Using-jQuery-with-ASP-NET.aspx
现在的问题是,正在(根据萤火虫)返回我的数据在此屏幕截图所示:
The problem is, my data is being returned as seen in this screen shot (according to firebug):
$("#btnGet").click(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "TimeService.svc/GetCar",
data: "{}",
dataType: "json",
success: function (data) {
alert(data.d);
}
});
});
});
我的Web服务的方法是这样的:
My Web Service method looks like this:
[OperationContract]
public string GetCar()
{
using (var sqlc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CarTracker.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"))
{
sqlc.Open();
var cmd = sqlc.CreateCommand();
cmd.CommandText = "SELECT CarID, CarName FROM tblCars";
using (var reader = cmd.ExecuteReader())
{
string sCar = "";
int testcount = 1;
for (int i = 0; i < testcount; i++)
{
reader.Read();
sCar += reader["CarName"].ToString();
}
return sCar; // Car_1
}
}
}
所以,我的问题是:
So my questions are:
-
在哪里在Firebug的D来 从?
Where does the 'd' in firebug come from?
我如何打造JSON式 根据我的分贝串返回 回jQuery的AJAX功能?
How do I build 'JSON-style' "strings" based on my db to return back to the jquery ajax function?
在理想情况下我会wantthe jQuery的AJAX的数据看起来是这样的:
Ideally I would wantthe jquery ajax data to look something like this:
{"TotalCars": x, "CarList":[{"CarName":"x1", "CarID":"id1"},{"CarName":"x2", "CarID":"id2"}]}
于是使用jQuery,我可以做的事情一样警报(data.TotalCars);
和所有诸如此类的东西
So then with jquery I can do things like alert(data.TotalCars);
and all that sort of stuff.
请记住,我-very-新本,所以我AP preciate任何帮助,您可以提供。 先感谢您! 3;
Please bear in mind that I'm -very- new to this so I appreciate any help you can provide. Thank you in advance! <3
推荐答案
所使用的Web服务框架,以确保一个裸体阵列永远不会被一个服务返回的D。这样做是为了解决潜在的交上门的Javascript开发。
The "d" is used by the webservice framework to ensure that a naked array is never returned by a service. This is done to work around a potential cross-site Javascript exploit.
您要创建描述您的数据合同类,例如:
You want to create classes that describe your data contract, for example:
[DataContract]
public class CarCollection {
[DataMember]
public int TotalCars { get { return CarList.Count; }}
[DataMember]
public List<Car> CarList { get; set; }
}
[DataContract]
public class Car {
[DataMember]
public string CarName { get; set; }
[DataMember]
public string CarId { get; set; }
}
然后,你会建立使用这些类的返回值。你也可以告诉WCF接受HTTP GET方法和JSON序列化与 WebGet
属性响应:
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
public string GetCar()
{
// You will probably build this up from your databas
var cars = new CarCollection { CarList = new List<Car>() {
new Car { CarName = "x1", CarId = "id1" },
new Car { CarName = "x2", CarId = "id2" },
new Car { CarName = "x3", CarId = "id3" },
}};
return cars;
}
WCF会自动将您的对象图序列化为JSON,并将其发送回客户端。
WCF will automatically serialize your object graph into JSON and send it back to the client.
您也可以再使用简化的JQuery方法 GET
:
You can also then use the simplified JQuery method get
:
$("#btnGet").click(function () {
$.get("TimeService.svc/GetCar", function(data){
alert(data);
});
});
这篇关于创建JSON回报和QUOT;串&QUOT;从web服务与jQuery的AJAX应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!