首先,我是一名新开发人员,所以如果我遗漏了一些明显的内容,我先向您道歉。
我正在开发一个Web应用程序,以便脱机使用IndexedDB中的大量数据。当用户转到Web应用程序时,客户端将从服务器获取整个数据库,并将其存储在indexeddb中使用。效果很好,但是当我尝试使用post方法将数据(再次记录)发送回WCF时,尝试发送ajax正文参数以及使用时,我得到的方法不允许或请求错误uri参数,它会命中服务器,但并非所有数据都会发送。我认为无效字符可能是一个因素,因此我在javascript中使用了encodeURIComponent方法将无效字符转换为uri参数中的有效字符。我也尝试过使用名为LZString的javascript压缩API压缩数据。我尝试使用XMLHttpRequest(我不完全了解)。
我还使用了一个称为Dexie.js的IndexedDB包装器。
我的代码示例如下。注释了一些代码,但剩下来显示我尝试过的内容。
这就是我在服务器上拥有的..
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "REST_SendCompletedServiceOrders",
BodyStyle = WebMessageBodyStyle.Wrapped)]
[FaultContract(typeof (Exception))]
bool REST_SendCompletedServiceOrders(string compressedWebData);
这是客户端上用于同步回来的click事件。
$('#syncCompletedData').on('click', function() {
db.ServiceOrder
.toArray(function(so) {
var completedServiceOrders = [];
for (var i = 0; i < so.length; i++) {
if (so[i].IsCompleted) {
completedServiceOrders.push(so[i]);
};
}
var customerId = sessionStorage.getItem("customerId");
var companyId = sessionStorage.getItem("companyId");
var computerId = sessionStorage.getItem("computerId");
var webData = JSON.stringify({ webCustomerId: customerId, webCompanyId: companyId, webComputerId: computerId, webServiceOrder: completedServiceOrders });
alert(webData);
alert("before compression is " + webData.length);
var URIEncodedWebData = encodeURIComponent(webData);
var JSONWebData = JSON.stringify(URIEncodedWebData);
var compressedWebData = LZString.compressToUTF16(JSONWebData);
alert("after compression is " + compressedWebData.length);
debugger;
try {
$.ajax({
type: "POST",
url: "MFSRemoteDataService/REST_SendCompletedServiceOrders",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { compressedWebData: compressedWebData },
success: function(data) { alert(JSON.stringify(data)); },
failure: function(errMsg) {
alert(errMsg);
}
});
} catch (e) {
alert(e);
}
});
});
压缩前的数据长度为7707。压缩后的数据长度为1831。
在此先感谢您的帮助,反馈,批评等。
我发现了我的问题。我一直在尝试将字符串传递给contract方法,但我一直收到错误的请求错误。相反,我包装了Json字符串并将其发送到对象,而不是我在服务器上创建的字符串。
我包装了JSON并将其发送到ajax请求的正文中。
var rawWebData = {
WebCustomerID: customerId,
WebCompanyID: companyId,
WebComputerID: computerId,
WebServiceOrders: completedServiceOrders
};
var rawData = { webData: rawWebData };
var webData = JSON.stringify(rawData);
try {
$.ajax({
type: "POST",
url: "MFSRemoteDataService/REST_SendCompletedServiceOrders",
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional: true,
data: webData,
success: function (data) {
alert(JSON.stringify(data));
},
failure: function (errMsg) {
alert(errMsg);
}
});
} catch (e) {
alert(e);
}
});
然后我创建了一个类来收集数据...
[DataContract]
public class WebServiceOrder
{
[DataMember]
public Int32 WebCustomerID { get; set; }
[DataMember]
public Int32 WebCompanyID { get; set; }
[DataMember]
public Int32 WebComputerID { get; set; }
[DataMember]
public virtual List<ServiceOrder> WebServiceOrders { get; set; }
}
然后,我更改了合同方法以接受我创建的对象而不是字符串。WCF反序列化JSON字符串。
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "REST_SendCompletedServiceOrders",
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[FaultContract(typeof (Exception))]
bool REST_SendCompletedServiceOrders(WebServiceOrder webData);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句