[Rest]Microsoft Dynamics CRM 2011的JS开发REST操作精简封装,增删改查集合

/*==================================================================
2012-08-09 victorlu@frensworkz.com


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  victorlu@frensworkz.com
-------------------------------------------------------------------
方法: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  victorlu@frensworkz.com
-------------------------------------------------------------------
方法: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  victorlu@frensworkz.com
-------------------------------------------------------------------
方法: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  victorlu@frensworkz.com
------------------------------------------------------------------
方法: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  victorlu@frensworkz.com
------------------------------------------------------------------
方法: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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值