/*==================================================================
2012-08-09
[email protected]
modifiedOn 12/9/2013 11:25AM
--------------------------------------------------------------------
fwREST v0.5.0
fwREST是用于CRM2011中使用REST访问OData服务进行数据操作的帮助js函数集。
使用时请用如下方式进行实例化:
var r = new fwREST();
或
var r = new fwREST(url);
如果返回结果对象的.error != undefined 刚表明有错误,请使用 .error.message.value获取错误内容。
===================================================================*/
function fwREST(url) {
var self = this;
self.ServerUrl = null;
if (url) {
self.ServerUrl = url;
} else {
if (window.Xrm) {
self.ServerUrl = Xrm.Page.context.prependOrgName("");
}
}
self.Async = false;
self.nextUrl = null;
}
/*=================================================================
2012-02-29
[email protected]
-------------------------------------------------------------------
方法:fwREST.create(entityname,jsondata)
说明:发送json形式的请求,用于创建记录。
entityname 字符串:实体名,需要使用逻辑名,区分大小写。如Account,New_test等
jsondata 字符串:JSON形式的请求内容,如"{'Name':'张三'}"
返回:object类型
==================================================================*/
fwREST.prototype.create = function (entityname, jsondata, callback) {
var requestURL = this.ServerUrl + "/XRMServices/2011/OrganizationData.svc/" + entityname + "Set";
//var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
var xmlhttp = new XMLHttpRequest();;
xmlhttp.open("POST", requestURL, this.Async);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length", jsondata.length);
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.send(jsondata);
var jsonObject = eval("(" + xmlhttp.responseText + ")");
if (jsonObject.error == undefined) {
return jsonObject.d;
} else {
return jsonObject;
}
}
/*=================================================================
2012-07-22
[email protected]
-------------------------------------------------------------------
方法:fwREST.delete(entityname,objectid)
说明:发送json形式的请求,用于创建记录。
entityname 字符串:实体名,需要使用逻辑名,区分大小写。如Account,New_test等
objectid 记录ID。
返回:无
==================================================================*/
fwREST.prototype.del = function (entityname, objectid, callback) {
var requestURL = this.ServerUrl_ + "/XRMServices/2011/OrganizationData.svc/" + entityname + "Set(guid'" + objectid + "')";
var xmlhttp = new XMLHttpRequest();;
xmlhttp.open("POST", requestURL, this.Async);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length", 0);
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.setRequestHeader("x-http-method", "DELETE");
xmlhttp.setRequestHeader("Accept-Encoding", "gzip, deflate");
xmlhttp.onreadystatechange = function () { fwrest_delete_callback(xmlhttp, entityname, objectid, callback); };
xmlhttp.send();
}
function fwrest_delete_callback(req, entityname, objectid, callback) {
if (req.readyState == 4 /* complete */) {
if (req.status == 204 || req.status == 1223) {
if (callback != null) {
callback(true);
}
}
else {
var jsonObject = eval("(" + req.responseText + ")");
if (req.responseText == "") {
if (callback != null) {
callback(true, entityname, objectid);
}
}
if (jsonObject.length > 0 && jsonObject.error == undefined) {
if (callback != null) {
callback(true, entityname, objectid);
}
} else {
if (callback != null) {
callback(jsonObject, entityname, objectid);
}
}
}
}
}
/*=================================================================
2012-02-29
[email protected]
-------------------------------------------------------------------
方法:fwREST.update(entityname,id,jsondata)
说明:发送json形式的请求,用于更新记录。
参数:
entityname 字符串:实体名,需要使用逻辑名,区分大小写。如Account,New_test等
id GUID,记录 ID
jsondata 字符串:JSON形式的请求内容,如"{'Name':'张三'}"
返回:无
==================================================================*/
fwREST.prototype.update = function (entityname, id, jsondata, callback, args) {
var requestURL = this.ServerUrl + "/XRMServices/2011/OrganizationData.svc/" + entityname +
"Set(guid'" + id + "')";
var xmlhttp = new XMLHttpRequest();;
xmlhttp.open("POST", requestURL, this.Async);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length", jsondata.length);
xmlhttp.setRequestHeader("x-http-method", "MERGE");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.setRequestHeader("Accept-Encoding", "gzip, deflate");
xmlhttp.eventArgs = args;
_restCurrentRestObject = this;
xmlhttp.onreadystatechange = function () { fwrest_update_callback(xmlhttp, callback, xmlhttp.eventArgs); };
xmlhttp.send(jsondata);
}
function fwrest_update_callback(req, callback, args) {
if (req.readyState == 4 /* complete */) {
if (req.status == 204 || req.status == 1223) {
if (callback != null) {
callback(true, args);
}
}
else {
var jsonObject = eval("(" + req.responseText + ")");
if (jsonObject.error == undefined) {
if (callback != null) {
callback(true, args);
}
} else {
if (callback != null) {
callback(jsonObject, args);
}
}
}
}
}
/*================================================================
2011-10-28
[email protected]
------------------------------------------------------------------
方法:fwREST.get(parameter)
说明:能过REST参数做查询并返回结果。
参数:
parameter 字符串。参数内容,如"AccountSet?$select=Name"
返回:object类型
=================================================================*/
fwREST.prototype.get = function (parameter, callback) {
var requestURL = this.ServerUrl + "/XRMServices/2011/OrganizationData.svc/" + parameter;
var xmlhttp = new XMLHttpRequest();;
xmlhttp.open("GET", requestURL, this.Async);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length", 0);
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.send(null);
var jsonObject = eval("(" + xmlhttp.responseText + ")");
if (jsonObject.error != undefined) {
return jsonObject;
} else {
if (jsonObject.d.results != undefined) {
var rspdata = jsonObject.d.results;
if (jsonObject.d.__next != undefined) {
this.nextUrl = jsonObject.d.__next;
}
return rspdata;
return;
} else {
return jsonObject.d;
}
}
}
/*================================================================
2014-3-17
[email protected]
------------------------------------------------------------------
方法:fwREST.getNext(callback)
说明:获取下一页数据。
参数:
callback 回调函数
返回:object类型或数组
=================================================================*/
fwREST.prototype.getNext = function (callback) {
var requestURL = this.nextUrl;
var xmlhttp = new XMLHttpRequest();;
xmlhttp.open("GET", requestURL, this.Async);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.setRequestHeader("Content-Length", 0);
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.send(null);
var jsonObject = eval("(" + xmlhttp.responseText + ")");
if (jsonObject.error != undefined) {
return jsonObject;
} else {
if (jsonObject.d.results != undefined) {
var rspdata = jsonObject.d.results;
if (jsonObject.d.__next != undefined) {
this.nextUrl = jsonObject.d.__next;
}
return rspdata;
} else {
return jsonObject.d;
}
}
}