Prev: Placing controls after a gridview
Next: Javascript code to recursively search for a element given the element's Id
From: Alex Maghen on 22 May 2010 10:58 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
From: Zhi-Qiang Ni[MSFT] on 24 May 2010 09:52 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 ==================================================
From: Alex Maghen on 24 May 2010 19:51 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 > ================================================== > > . >
From: Zhi-Qiang Ni[MSFT] on 26 May 2010 00:46
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 |