0

I have this method in asp.net ajax enabled mvc service

[OperationContract]
public TestRun GetTestSuite(string testSuitName) {
   TestRun testRun = 
      AdapterFactory.CreateTestRunAdapter().GetByTestSuiteName("testSuit");
   return testRun;
} 

Where TestRun is Created using entities framework against TestRun table.

I am using following ajax method

function getTestSuite() {
            $.ajax({
                type: "POST",
                url: "Services/TestRunService.svc/GetTestSuite",
                data: '{"testSuitName" : "' + testSuiteName + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                },

                //If the call fails
                error: function (xhr, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            })
        }

I get undefined error. If I return string from service it works. Do I need to change

dataType: "json" to something else ?

Regards, Asif Hameed

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
asif
  • 43
  • 3
  • 10

1 Answers1

0

Is the TestRun class marked with the [DataContract] attribute?

More info here

DannyLane
  • 2,096
  • 1
  • 16
  • 17
  • But it must have some properties that contain the data you want to send to the client? – DannyLane Feb 22 '11 at 10:53
  • Hi Danny: yes it contains this attribute. I just created the datamodel using entity framework and this class is created with properties. it has this attribute [DataContractAttribute(IsReference=true)] – asif Feb 22 '11 at 10:55
  • Hi @DannyLane: Any suggestion ? Do I need to create another class with name TestRun and with properties only instead of using entitiesframework class ? – asif Feb 22 '11 at 11:05
  • If the TestRun class is an entity from the Entity Framework then I don't think you will be able to setialize it like this. This question has more info. http://stackoverflow.com/questions/657939/serialize-entity-framework-objects-into-json I would use the approach in the question above or consider creating a POCO for your DataContract – DannyLane Feb 22 '11 at 11:13
  • Hi @DannyLane: I used below code but getting circular refrence error. Can you please suggest me how to change this method in sevice. [OperationContract] public string GetTestSuite(string testSuitName) { string jsonClient = null; var testRun = AdapterFactory.CreateTestRunAdapter().GetByTestSuiteName("testSuit"); JavaScriptSerializer s = new JavaScriptSerializer(); jsonClient = s.Serialize(testRun); return jsonClient; } – asif Feb 22 '11 at 11:26
  • The issue you have is that you are trying to serialize an instance of the TestRun class, this from the Entity Framework so it can't be serialized like you are trying to do. You can either create a new class to hold the data you want and pass that down or copy the data from the TestRun instance into a dynamic object and pass that down. – DannyLane Feb 22 '11 at 12:58