I have an Ajax function called from JQuery that goes to a webservice to return a value. I need a SIMPLE example on how I can do this. I've been going nuts with serializing and every other aspect of this topic.
I need to return either an ArrayList with ONE string field or a DataTable of some kind. Either way, I'm populating it into a DropDownList.
I'm willing to consider alternatives to this idea. (Background info - I get a value from a textbox and I need to run it through a DB to get an associated value or set of values). I'm being really general so that someone can show a simple example. Thanks.
no, the sample will not work. version 2.0 does not support javascript web methods (or generating the javascript method includes). this is as close as you can come:
or you can use jquery ajax, and call a page that just returns (via response.write) the correct data. with jquery you could do a form post that returned json data. .net 2.0 has a json serializer you can use to produce the response.
I had a feeling it wouldn't work. I've been doing a lot of research and I've noticed that "msg.d" or "whatever.d" is not supported in .NET 2.0. I'll read up on that article. Looks like I need JSon.js somehow. Is that right?
I return a simple ArrayList to my Ajax function. When I do an alert, the value looks like this: ["string1","string2"]
I haven't seen exact examples on how to handle this, but I need to iterate through this and place each value into a DropDownList. If I do a $.each on the list, it iterates through EVERY CHARACTER, not every string. Can I set the DataSource of the DDL to
this array?
robert6703
Member
2 Points
13 Posts
Simple return value from WebMethod from Ajax function using JQuery
Sep 22, 2010 06:28 PM|LINK
I have an Ajax function called from JQuery that goes to a webservice to return a value. I need a SIMPLE example on how I can do this. I've been going nuts with serializing and every other aspect of this topic.
I need to return either an ArrayList with ONE string field or a DataTable of some kind. Either way, I'm populating it into a DropDownList.
I'm willing to consider alternatives to this idea. (Background info - I get a value from a textbox and I need to run it through a DB to get an associated value or set of values). I'm being really general so that someone can show a simple example. Thanks.
JQuery & Asp.net
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 22, 2010 06:50 PM|LINK
Please look the example provided by Mikes: http://www.mikesdotnetting.com/Article/96/Handling-JSON-Arrays-returned-from-ASP.NET-Web-Services-with-jQuery
robert6703
Member
2 Points
13 Posts
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 22, 2010 07:21 PM|LINK
Thanks for your reply, but should this example work if I'm using Visual Studio 2005 (.Net 2.0) ?
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 22, 2010 09:08 PM|LINK
Should work.
bruce (sqlwo...
All-Star
36822 Points
5441 Posts
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 22, 2010 09:59 PM|LINK
no, the sample will not work. version 2.0 does not support javascript web methods (or generating the javascript method includes). this is as close as you can come:
http://msdn.microsoft.com/en-us/library/bb299886.aspx
or you can use jquery ajax, and call a page that just returns (via response.write) the correct data. with jquery you could do a form post that returned json data. .net 2.0 has a json serializer you can use to produce the response.
robert6703
Member
2 Points
13 Posts
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 23, 2010 01:17 PM|LINK
I had a feeling it wouldn't work. I've been doing a lot of research and I've noticed that "msg.d" or "whatever.d" is not supported in .NET 2.0. I'll read up on that article. Looks like I need JSon.js somehow. Is that right?
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 23, 2010 01:25 PM|LINK
".d" introduced in ASP.NET AJAX 3.5’s JSON responses. Please read this:
http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/
robert6703
Member
2 Points
13 Posts
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 23, 2010 02:35 PM|LINK
This actually helped! Thanks!
I return a simple ArrayList to my Ajax function. When I do an alert, the value looks like this: ["string1","string2"]
I haven't seen exact examples on how to handle this, but I need to iterate through this and place each value into a DropDownList. If I do a $.each on the list, it iterates through EVERY CHARACTER, not every string. Can I set the DataSource of the DDL to this array?
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 23, 2010 03:05 PM|LINK
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script> </head> <body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> <input id="btnFoo" type="button" value="Test" /> <script type="text/javascript"> $('#btnFoo').click(function () { var returnedData = { "Table": [{ "value": "1", "name": "One" }, { "value": "2", "name": "Two" }, { "value": "3", "name": "Three" }, { "value": "4", "name": "Four" }, { "value": "5", "name": "Five" } ] } var html = ""; for (var i = 0; i < returnedData.Table.length; i++) { html += "<option value='" + returnedData.Table[i].value + "'>" + returnedData.Table[i].name + "</option>"; } $('#<%= DropDownList1.ClientID %>').html(html); }); </script> </form> </body> </html>robert6703
Member
2 Points
13 Posts
Re: Simple return value from WebMethod from Ajax function using JQuery
Sep 23, 2010 03:52 PM|LINK
Looks simple enough, but I'm such a novice - I can't see it being applied in my situation. Can you help?
In Webmethod: While reading using an SQLDataReader, I do: list.Add(rdr["pmsid"].ToString());
and I return the "list", which is an ArrayList.
Output from webmethod:
<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://microsoft.com/webservices/">
<anyType xsi:type="xsd:string">0614747</anyType>
<anyType xsi:type="xsd:string">0980142</anyType>
</ArrayOfAnyType>
In the Ajax function: I do an alert on the result and get: ["0614747","0980142"]
In my example I do not have the curly brackets or the same structure you are using. Any suggestions in my situation?