Full "Loop" jSON Example?

Discussion in 'ASP .Net' started by Alex Maghen, May 22, 2010.

  1. Alex Maghen

    Alex Maghen Guest

    I am trying to do something which would seem so simple:

    I have a simple Class in ASP.NET that I want to set up to be called and
    consumed by a jQuery $.getjson() in a client-side JavaScript.

    The Class I want to serialize to json as very simple (except that it has two
    fields that are DateTime's and I've heard they're complicated).

    I'd love a sample (if anyone has one) of the full code of the ASPX that
    serializes and then delivers json (properly ContentType'd), AND a sample HTML
    with the jQuery that $getjson()'s it and uses it.

    I am having such a hard time with this and I just need a full sample to get
    me going!

    Many thanks.

    Alex
    Alex Maghen, May 22, 2010
    #1
    1. Advertising

  2. Hi Alex Maghen,

    $.getJSON(url,data,callback)
    Similar to $.post(), but expects the result to be JSON which is
    automatically deserialized into a Javascript value and passed to the
    callback as a parameter. While this function is handy for simple JSON
    results there are two things to watch out for: Dates are not parsed since
    there's no date literal in Javascript, so whatever the server returns will
    be used (typically a string). $.getJSON() also doesn't support JSON POST
    data - only POST encoded variables. This function is useful for simple JSON
    results returned from arbitrary services, but not usable for calling WCF or
    ASMX ASP.NET services since they expect JSON POST input.

    If you have already use the WCF or ASMX service, I recommend that you use
    the ASP.NET AJAX which support client side JSON serialization natively.
    http://msdn.microsoft.com/en-us/library/bb924552.aspx
    http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

    However, if you insist on using only jQuery to call WCF, special handling
    is required for deserialization.
    Please refer to this article:
    http://www.west-wind.com/presentations/jquery/jquerypart2.aspx#WCFASMX .

    --
    Sincerely,
    Zhi-Qiang Ni
    Microsoft Online Support
    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    MSDN Managed Newsgroup support offering is for non-urgent issues where an
    initial response from the community or a Microsoft Support Engineer within
    2 business day is acceptable. Please note that each follow up response may
    take approximately 2 business days as the support professional working with
    you may need further investigation to reach the most efficient resolution.
    The offering is not appropriate for situations that require urgent,
    real-time or phone-based interactions. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    Zhi-Qiang Ni[MSFT], May 24, 2010
    #2
    1. Advertising

  3. Alex Maghen

    Alex Maghen Guest

    Thanks so much for the reply. Yes, I must use jQuery on the client-side (or
    rather, I cannot assume that I can use anything from Microsoft because the
    consumer of my json will not necessarily be under my control.

    I've had a lot of success since I wrote you last, but I'm left with one more
    problem: Any DateTime values that I serialize with "JavaScriptSerializer" are
    output in a format the isn't parsed properly in jQuery. I can write a script
    on the client side to clean this up, but I'd much rather have it created
    cleanly in a way that is perfectly consumable and parseable in jQuery.
    Everything else is workign fine, except for DateTime's. What can I do about
    this?

    Many thanks.

    Alex

    "Zhi-Qiang Ni[MSFT]" wrote:

    > Hi Alex Maghen,
    >
    > $.getJSON(url,data,callback)
    > Similar to $.post(), but expects the result to be JSON which is
    > automatically deserialized into a Javascript value and passed to the
    > callback as a parameter. While this function is handy for simple JSON
    > results there are two things to watch out for: Dates are not parsed since
    > there's no date literal in Javascript, so whatever the server returns will
    > be used (typically a string). $.getJSON() also doesn't support JSON POST
    > data - only POST encoded variables. This function is useful for simple JSON
    > results returned from arbitrary services, but not usable for calling WCF or
    > ASMX ASP.NET services since they expect JSON POST input.
    >
    > If you have already use the WCF or ASMX service, I recommend that you use
    > the ASP.NET AJAX which support client side JSON serialization natively.
    > http://msdn.microsoft.com/en-us/library/bb924552.aspx
    > http://msdn.microsoft.com/en-us/magazine/cc163499.aspx
    >
    > However, if you insist on using only jQuery to call WCF, special handling
    > is required for deserialization.
    > Please refer to this article:
    > http://www.west-wind.com/presentations/jquery/jquerypart2.aspx#WCFASMX .
    >
    > --
    > Sincerely,
    > Zhi-Qiang Ni
    > Microsoft Online Support
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > MSDN Managed Newsgroup support offering is for non-urgent issues where an
    > initial response from the community or a Microsoft Support Engineer within
    > 2 business day is acceptable. Please note that each follow up response may
    > take approximately 2 business days as the support professional working with
    > you may need further investigation to reach the most efficient resolution.
    > The offering is not appropriate for situations that require urgent,
    > real-time or phone-based interactions. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    >
    > .
    >
    Alex Maghen, May 25, 2010
    #3
  4. Hi Alex Maghen,

    As my last reply, there is an article for deserializing the JSON from the
    MS DateTime Format.
    http://www.west-wind.com/presentations/jquery/jquerypart2.aspx#WCFASMX .

    The related information of it ------------------
    MS AJAX aware JavaScript JSON Serializer
    jQuery does not include a JSON serializer. Since WCF/ASMX require
    parameters to be sent as POST JSON objects, a serializer is required on the
    client. The standard getJSON() deserialization also doesn¡¯t work with the
    MS date format ("LastQuoteTime":"\/Date(1227578400000)\/") so special
    handling is required for deserialization as well. For this task I¡¯ve
    provided a modified version of Douglas Crockford¡¯s JSON2.js or
    ww.jQuery.js both of which include JSON parsers that understand the
    Microsoft date format both for serialization and deserialization.

    This response contains a reference to a third party World Wide Web site.
    Microsoft is providing this information as a convenience to you. Microsoft
    does not control these sites and has not tested any software or information
    found on these sites; therefore, Microsoft cannot make any representations
    regarding the quality, safety, or suitability of any software or
    information found there. There are inherent dangers in the use of any
    software found on the Internet, and Microsoft cautions you to make sure
    that you completely understand the risk before retrieving any software from
    the Internet.

    --
    Sincerely,
    Zhi-Qiang Ni
    Microsoft Online Support
    Zhi-Qiang Ni[MSFT], May 26, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    237
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    323
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    248
    dieter
    Feb 15, 2013
  4. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    259
    Bryan Britten
    May 28, 2013
Loading...

Share This Page