WCF for Json / Jquery获取状态列表.. [英] WCF for Json /Jquery to get a list of states..
问题描述
我正在尝试从WCF返回通用列表。但返回未知错误。任何人都可以帮我解决这个问题。
IService.cs中的
==============
[ServiceContract]
公共接口IServiceLP18
{
[OperationContract]
[WebInvoke(Method =POST) ,BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]
List< lp18wcf.servicelp18.statecls> GetAllStates();
}
ServiceLP18中的
=========== ==
I am trying to return a generic list from a WCF. But return an "unknown" error. Can anyone help me out to solve this.
in IService.cs
==============
[ServiceContract]
public interface IServiceLP18
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
List<lp18wcf.servicelp18.statecls> GetAllStates();
}
in ServiceLP18
=============
public class ServiceLP18 : IServiceLP18
{
public class StateCls
{
public string StateID;
public string StateName;
}
public List<StateCls> GetAllStates()
{
List<StateCls> StateList = new List<StateCls>();
for (int i = 0; i < 10; i++)
{
StateCls tempRow = new StateCls();
tempRow.StateID = i.ToString();
tempRow.StateName = "Sate " + i.ToString();
StateList.Add(tempRow);
}
return StateList;
}
}
========================= ===========
在calling.html页面
== ==================
====================================
In calling.html page
====================
function CallGetStates() {
$.ajax({
Type : "POST",
url: "http://localhost:50609/ServiceLP18.svc/GetAllStates",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata: true,
success: function (response) {
var statess = response.d;
$.each(statess, function (index, LpStates) {
alert(LpStates.StateName);
});
},
error: ServiceFailed// When Service call fails
});
}
推荐答案
.ajax({
类型: POST,
url: http:// localhost:50609 / ServiceLP18.svc / GetAllStates,
data: {},
contentType: application / json; charset = utf-8 ,
dataType: jsonp,
processdata: true ,
成功:功能(响应){
var statess = response.d;
.ajax({ Type : "POST", url: "http://localhost:50609/ServiceLP18.svc/GetAllStates", data: "{}", contentType: "application/json; charset=utf-8", dataType: "jsonp", processdata: true, success: function (response) { var statess = response.d;
.each(statess, function (i ndex,LpStates){
alert(LpStates.StateName);
});
},
错误:ServiceFailed // 服务调用失败时
});
}
.each(statess, function (index, LpStates) { alert(LpStates.StateName); }); }, error: ServiceFailed// When Service call fails }); }
JSON版本不起作用,因为它是一个跨源调用(参见:同源策略: http://en.wikipedia.org/wiki/Same_origin_policy [ ^ ])。 Google URL缩短程序是否具有JSON-P API?它必须明确支持它。 (另外,JSON-P不能POST;本质上它是GET。)
更新:看起来他们不支持它但是,有一个开放的增强请求。
https: //code.google.com/p/yourls/issues/detail?id=744 [ ^ ]
The JSON version doesn''t work because it''s a cross-origin call (see: Same Origin Policy: http://en.wikipedia.org/wiki/Same_origin_policy[^]). Does the Google URL shortener have a JSON-P API? It has to explicitly support it. (Also, JSON-P can''t be POST; by its nature it''s a GET.)
Update: Looks like they don''t support it yet, but there''s an open enhancement request.
https://code.google.com/p/yourls/issues/detail?id=744[^]
这篇关于WCF for Json / Jquery获取状态列表..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!