0

//准备'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。当我搜索时,我知道这个错误是由于跨站点脚本造成的。如何启用跨站点脚本或如何摆脱此错误。

4

1 回答 1

0
  function createAccount() {
var gid = Xrm.Page.getAttribute("new_syllabus").getValue();
var stamail = new Object();
stamail.new_name = "Maddy";
stamail.new_gid = gid;
var myurl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
//alert(gid);
// alert(Xrm.Page.data.entity.getId());

var jsoEntity = JSON.stringify(stamail);
var createRecordReq = new XMLHttpRequest();

var ODataPath = myurl + "/XRMServices/2011/OrganizationData.svc";

createRecordReq.open("POST", ODataPath + "/new_nitSet", false);
createRecordReq.setRequestHeader("Accept", "application/json");
createRecordReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

// createRecordReq.onreadystatechange = function () { requestCallBack(this); };
createRecordReq.send(jsoEntity);

var newRecord = JSON.parse(createRecordReq.responseText).d;

}

我没有使用 ajax,而是使用了上面的代码。它工作正常。

于 2012-10-08T09:54:53.660 回答