Hello Guys. I am new to jQuery and would like to know how I can save the result of a WebService call into variables using jQuery. The WebService returns an Object of type CProduct and it queries the database and returns the object in response.d because I
can view the result in FireBug but I am stuck after that point.
$("#btnLookup").click (function(event) {
var mCode = $("#item_code").val();
var mData = "{ 'vCode' : '" + mCode + "' }";
// How do I refer to the object and its property members from here on
// Each time I try to reference the object's members, I either get "[object Object]" as the result or
// nothing is displayed.
// I have the idea that I can probably do the following, but it does not work.
// Could it be because probably because I want to evaluate a CProduct object? I don't know and need some help.
seanyp
Member
1 Points
7 Posts
Save JSON Result from WebService into variables
Sep 22, 2009 06:02 PM|LINK
Hello Guys. I am new to jQuery and would like to know how I can save the result of a WebService call into variables using jQuery. The WebService returns an Object of type CProduct and it queries the database and returns the object in response.d because I can view the result in FireBug but I am stuck after that point.
$("#btnLookup").click (function(event) {
var mCode = $("#item_code").val();
var mData = "{ 'vCode' : '" + mCode + "' }";
$.ajax( {
type: "POST" ,
url: "ProductService.asmx/getSingleProduct" ,
data: mData ,
contentType: "application/json; charset=utf-8" ,
dataType: "json" ,
success: function (response) {
// How do I refer to the object and its property members from here on
// Each time I try to reference the object's members, I either get "[object Object]" as the result or
// nothing is displayed.
// I have the idea that I can probably do the following, but it does not work.
// Could it be because probably because I want to evaluate a CProduct object? I don't know and need some help.
var oProducts = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
$("#pnlProducts").empty();
$("#pnlProducts").append("<p><strong>Product Name: </strong>" + oProducts.name_val + "<br /> " +
"<strong>Item Description: </strong>" + oProducts.Description + "<br /> " +
"<strong>Quantity: </strong>" + oProducts.qty_val + "<br />" +
}
)};
)};
<div style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;" id="_mcePaste"> )};</div>JSON webservice json ajax asp.net ajax webservice web service response
gt1329a
All-Star
15377 Points
2501 Posts
ASPInsiders
MVP
Re: Save JSON Result from WebService into variables
Sep 23, 2009 03:23 AM|LINK
You shouldn't need that eval(). jQuery does that automatically when you set the dataType to "json".
What does the Product class you're returning look like?
A guide to combining jQuery and ASP.NET: jQuery for the ASP.NET developer
seanyp
Member
1 Points
7 Posts
Re: Save JSON Result from WebService into variables
Sep 23, 2009 04:13 PM|LINK
You are absolutely correct. I don't need to use the eval() finction. Nor do I refer to response.d. All I had to do was :
var objProduct = response;
And then I can access the members as expexted by;
objProduct.name_val
So if I wanted to put the return value into a textbox with ID= tbName all I have to do is the following:
$("#tbName").val(objProduct.Name);