//准备'Account'对象并调用create函数
function createAccount() {
var new_nit = new Object();
// Set text field
new_nit.Name = "Maddy";
createRecord(new_nit, "new_nitSet", createAccountCompleted, null);
}
// 此回调方法在成功创建帐户时执行
function createAccountCompleted(data, textStatus, XmlHttpRequest) {
var nit = data;
alert("Account created; Id: ");
}
// 此函数通过调用 OData 创建记录
function createRecord(entityObject, odataSetName, successCallback, errorCallback) {
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(entityObject);
// Get Server URL
var serverUrl = Xrm.Page.context.getServerUrl();
//The OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName,
data: jsonEntity,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
if (successCallback) {
successCallback(data.d, textStatus, XmlHttpRequest);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (errorCallback)
errorCallback(XmlHttpRequest, textStatus, errorThrown);
else
alert("Error on the creation of record; Error – "+errorThrown);
}
});
}
我正在使用上面的代码创建一个名为nit
. 我在网络资源中有 json2 和 jQuery js 文件。当我在按钮单击时运行此代码时,我收到错误消息No Transport
。当我搜索时,我知道这个错误是由于跨站点脚本造成的。如何启用跨站点脚本或如何摆脱此错误。