0

I have a web application. Inside I have a asmx file “MyWebServices.asmx” where I have a Webmethod that Sends a json object to my WebForm2.aspx. My problem is how to capture this object with Javascript store it and display it with javascript. My code on MyWebServices.asmx :

public class apointment
    {
        public string Fname{ get; set; }
        public string Lname{ get; set; }
        public string customerid { get; set; }

    }

[WebMethod]
    public string myapointment()
    {

        apointment myapointment1= new apointment();
       myapointment1.customerid = "123POW";
        myapointment1.Fname = "John";
        myapointment1.Lname = "JohnsLname";
        System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        string sJSON = oSerializer.Serialize(myapointment1);
        return sJSON;
    }

My code on .net page Javascript:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "services/MyWebServices.asmx/myapointment",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {



                // Insert the returned HTML into the <div>.
                var myrant = data.d;

                $('#RSSContent').html(data.d);
            }
        });
    });


</script>

The problem is with this code I am taking a string:

{"Fname":"John","Lname":"JohnsLname","customerid":"123POW"}

How can I convert this string to an object type appointment? Am asking because after that I can displayed correctly on html, I would like to create lists of appointments.

13
  • in myapointment method return apointment object not string... Commented Aug 10, 2014 at 12:52
  • declare it so that it return apointment and mark your method with [ScriptMethod(ResponseFormat = ResponseFormat.Json)] Commented Aug 10, 2014 at 12:56
  • ok i did it. How can i access it with javascript? Commented Aug 10, 2014 at 12:56
  • data in success: function (data){ is your object... Commented Aug 10, 2014 at 12:56
  • I have changed my code to $('#RSSContent').html(data.Fname); and i am taking nothing Commented Aug 10, 2014 at 12:59

1 Answer 1

-1

Have you looked at using the following code for the web service.

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public apointment myapointment()
{
    apointment myapointment1 = new apointment();
    myapointment1.customerid = "123POW";
    myapointment1.Fname = "John";
    myapointment1.Lname = "JohnsLname";

    return myapointment1;
}

Secondly as per the answer to this question (Calling ASMX Web Service from Javascript), have you uncommented this line at the top of the web service.

//[System.Web.Script.Services.ScriptService]

Finally have you tried using Fiddler (http://www.telerik.com/fiddler) to debug your web service to determine whether the web service is returning the correct output? Use the Composer tab with the following settings:

You should see the request to the web service show up in the left-hand column of the page. Click on the Raw or TextView tabs and see if the JSON you are expecting is shown.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.