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.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
focus
  • 171
  • 9
  • 31
  • in `myapointment` method return `apointment` object not string... – L.B Aug 10 '14 at 12:52
  • declare it so that it return `apointment` and mark your method with `[ScriptMethod(ResponseFormat = ResponseFormat.Json)]` – L.B Aug 10 '14 at 12:56
  • ok i did it. How can i access it with javascript? – focus Aug 10 '14 at 12:56
  • `data` in `success: function (data){` is your object... – L.B Aug 10 '14 at 12:56
  • I have changed my code to $('#RSSContent').html(data.Fname); and i am taking nothing – focus Aug 10 '14 at 12:59
  • Use your browsers developer tools and see the content of `data`. – L.B Aug 10 '14 at 13:00
  • I am using firebug. I cant see the response. Also my question is how can i display only the Fname or the Lname? – focus Aug 10 '14 at 13:03
  • If you don't want to do debugging to see what goes wrong, I can not help you. Will you ask every problem you face to a stranger. Learn debugging... Good luck... – L.B Aug 10 '14 at 13:09
  • no, i am not asking to debug my code. i would like a sugestion on how i could only display the Fname property of the object. Thank you – focus Aug 10 '14 at 13:11
  • No I am asking you to debug your code.... – L.B Aug 10 '14 at 13:13
  • Thank you for your time, but i have no any results. I am asking here and i post my code to find a solution to my problem. If i could with my own i wouldn post this issue. Anyway, I found very helpfull this post:http://stackoverflow.com/questions/17953299/web-service-call-through-js-datatype-json?rq=1 – focus Aug 10 '14 at 13:26
  • That approach can work but is **wrong**!. You turn your object to string in your webmethod which in turns does the same thing again (string to jsonstring). So instead of getting the object directly from ajax (which does the deserialization by default) you need to do it again. So double-serialization + double-deserialization...... You can use it in your homework but don't use for professional apps.. – L.B Aug 10 '14 at 13:34
  • Ok Thank you for your time! I will have it in mind... If anyone else could help i will appreciate it. – focus Aug 10 '14 at 14:22

1 Answers1

-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.

Community
  • 1
  • 1
ACOMIT001
  • 510
  • 1
  • 7
  • 21