0

//Prepare ‘Account’ object and call create function

function createAccount() {

var new_nit = new Object();

// Set text field

new_nit.Name = "Maddy";
createRecord(new_nit, "new_nitSet", createAccountCompleted, null);

}

// This callback method executes on succesful account creation

function createAccountCompleted(data, textStatus, XmlHttpRequest) {

var nit = data;

alert("Account created; Id: ");

}

// This function creates record by making OData call

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);

}

});

}

I'm using above code to create a entity called nit. I have json2 and jQuery js files in web resource. When i run this code on button click, i'm getting error as No Transport. when i searched , i got to know that this error is because of cross site scripting. How to enable cross site scripting or how to get rid of this error.

Maddy
  • 263
  • 1
  • 9
  • 24
  • Here in this [link](http://stackoverflow.com/questions/5241088/jquery-call-to-webservice-returns-no-transport-error), they have given solution as adding `jQuery.support.cors = true;` code to enable cross site scripting. But 'm not getting where to add this line. If in jQuery.js then in which function i need to add. – Maddy Oct 08 '12 at 08:41
  • 1
    Add in the top line of your above function; should work fine. – glosrob Oct 08 '12 at 11:43

1 Answers1

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;

}

Instead of using ajax i used above code. Its working fine.

Maddy
  • 263
  • 1
  • 9
  • 24