我已经创建了一个接受并返回json数据的WCF服务。
我使用Fiddler对其进行了测试,它在json中正确返回字符串数组,但在XML中返回System.Data.DataSet。
我是否天真期待将数据集隐式转换为json?
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
DataSet GetDataset()
答案 0 :(得分:0)
我只是将我的WCF服务转换为使用xml中的json,(因此我可以使用javascript更轻松地调用它)。这一切都对我有用,如果你能从中收集任何东西,我祝你好。
这是在我的界面中(它返回一个包含最新版本的字符串进行比较)
[OperationContract]
[FaultContract(typeof(ExceptionFault))]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetLatestVersion();
然后我写了一个JS类来处理WCF调用
//
// Handles WCF calls and returns
//
function WCF(url) {
// Makes a call against a WCF service
//
// id: is a unique key to specify this service
//
// action: is the name of the method in the WCF service to call
//
// arguments: is a json object of the named argument values
// Example:
// { firstname: "John", lastname: "Albert" }
//
// completed: a function that will be called on successful completion of the call
// the result will be deserialized into a json object
// Format:
// function(jsonReturnObject)
//
// errored: a function that will be called on unsuccessful completion of the call
// the resulting status and error text will be sent
// Format:
// function(status, statusText)
this.call = function (id, action, arguments, completed, errored) {
// save key
this.keys[id] = true;
// Create HTTP request
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
alert("This sample only works in browsers with AJAX support");
}
var self = this;
// Create result handler
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
// Success!
if (typeof completed == 'function') {
if (self.keys[id]) {
completed(JSON.parse(xmlHttp.responseText)[action + "Result"]);
}
}
} else {
// Failed
if (typeof errored == 'function') {
if (self.keys[id]) {
errored(xmlHttp.status, xmlHttp.statusText);
}
}
}
// on termination wipe the key
delete self.keys[id];
}
}
// Build the operation URL
var requestUrl = this.url + "\\" + action;
// Build the body of the JSON message
var body = JSON.stringify(arguments);
// Send the HTTP request
xmlHttp.open("POST", requestUrl, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send(body);
};
// cancelds a specific existing call
this.cancel = function (id) {
if (this.keys[id] !== undefined) {
this.keys[id] = false;
}
}
//
// CONSTRUCTOR
//
this.url = url;
this.keys = new Object();
return this;
};
这可能超出了你的需要,要求是我可以取消一个电话(由于win8 app框架在我下面交换了我的html)。
MyService = new WCF('http://localhost:12345/MyService.svc');
拨打服务电话
HealthAppService.call('unique_id', 'GetLatestVersion', {}, function (response) {
// If server version is newer, display upgrade notification
var bits = response.split('.');
var ver = new Array(version.major, version.minor, version.revision, version.build);
var needUpdate = false;
for (var i = 0; i < 3; i++) {
if (ver[i] > parseInt[i]) break;
if (ver[i] < parseInt[i]) { needUpdate = true; break; }
}
if (needUpdate) {
// handle update
}
}, function (code, message) {
// Error
});