从启用 AJAX 的 WCF 服务加载 Handsontable
Posted
技术标签:
【中文标题】从启用 AJAX 的 WCF 服务加载 Handsontable【英文标题】:Loading Handsontable from an AJAX-enabled WCF Service 【发布时间】:2013-04-10 07:17:01 【问题描述】:我正在尝试在支持 AJAX 的 WCF 服务中显示 Handsontable 中的数据,但它没有显示任何内容。
CarsService.svc:
[DataContract]
public class Car
[DataMember]
public string Name get; set;
[DataMember]
public string Year get; set;
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CarsService
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public IEnumerable<Car> GetCars()
var cars = new List<Car>();
var car1 = new Car()
Name = "Kia",
Year = "2008"
;
var car2 = new Car()
Name = "Nissan",
Year = "2009"
;
cars.Add(car1);
cars.Add(car2);
return cars;
WebPage:
<head runat="server">
<title></title>
<script type='text/javascript' src='/Scripts/jquery-1.8.3.js'></script>
<script type='text/javascript' src="/Scripts/Handsontable/jquery.handsontable.js"></script>
<script type='text/javascript' src="/Scripts/Handsontable/bootstrap-typeahead.js"></script>
<script type='text/javascript' src="/Scripts/jquery.contextMenu.js"></script>
<script type='text/javascript' src="/Scripts/jquery.ui.position.js"></script>
<link rel="stylesheet" type="text/css" href="/Styles/jquery.contextMenu.css"/>
<link rel="stylesheet" type="text/css" href="/Styles/Handsontable/jquery.handsontable.css"/>
</head>
<body>
<form id="form1" runat="server">
<script type='text/javascript'>
$(window).load(function ()
$.ajax(
url: "/CarsService.svc/GetCars",
type: "GET",
dataType: "json"
)
.success(function (result)
$("#exampleGrid").handsontable("loadData", result);
)
.fail(function (r, o)
alert("Failed : " + r.responseText);
);
);
</script>
<div id="exampleGrid" class="dataTable"></div>
</form>
</body>
【问题讨论】:
导航到http://..../CarsService.svc/GetCars
时浏览器会显示什么
它询问我是否要保存文件。文件如下所示:"d":["__type":"Car:#GridWebApp","Name":"Kia","Year":"2008","__type":"Car:# GridWebApp","Name":"Nissan","Year":"2009"]
我忘了补充一点,当我运行应用程序时会调用 WCF 服务(也许这就是我收到 *** 错误的原因)。
您的 Handsontable 很可能需要不同的 json。我也会尝试将BodyStyle = WebMessageBodyStyle.Bare
添加到WebGet
属性,但更好的是,请参阅它的文档。
如果我添加这个:[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] 我得到一个错误:'WebScriptEnablingBehavior' 不支持正文样式'Bare'。将正文样式更改为“WrappedRequest”。
【参考方案1】:
以下内容对我有用:
我使用 newtonsoft 的 Json.net: http://json.codeplex.com/
服务合同:
[WebInvoke(Method = "GET")]
[OperationContract]
string GetAllData();
wcf 服务返回:
return JsonConvert.SerializeObject(data);
还有 javascript... (“数据”是我的 Ajax 调用返回的内容)
var myData = JSON.parse(data.d);
var config =
data: myData,
columns: [
//I had to set 'data' of each column
data: "OtherParty" ,
data: "Reference"
]
;
$container.handsontable(config);
希望对你有帮助
【讨论】:
【参考方案2】:加密工具
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEParameterSpec;
import sun.misc.BASE64Decoder;
public class EncryptionUtils
private Cipher decryptCipher;
private BASE64Decoder decoder = new BASE64Decoder();
public EncryptionUtils() throws SecurityException
Security.addProvider(new com.sun.crypto.provider.SunJCE());
char[] pass = "edurixkey".toCharArray();
byte[] salt =
-93, 33, 36, 44,
-14, -46, 62, 25 ;
int iterations = 3;
init(pass, salt, iterations);
public void init(char[] pass, byte[] salt, int iterations) throws SecurityException
try
PBEParameterSpec ps = new PBEParameterSpec(salt, 20);
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass));
decryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
decryptCipher.init(2, k, ps);
catch (Exception e)
throw new SecurityException("Could not initialize Encryption: " + e.getMessage());
public synchronized String decrypt(String str) throws SecurityException
try
byte[] dec = decoder.decodeBuffer(str);
byte[] utf8 = decryptCipher.doFinal(dec);
return new String(utf8, "UTF8");
catch (Exception e)
throw new SecurityException("Could not decrypt: " + e.getMessage());
【讨论】:
以上是关于从启用 AJAX 的 WCF 服务加载 Handsontable的主要内容,如果未能解决你的问题,请参考以下文章
启用从 jQuery ajax 到不在 IIS 中托管的 WCF 服务的 CORS POST