BKahuna
BKahuna

Reputation: 611

JSON return object for jQuery .post is null (when it's not)

I have a jQuery .post command that is calling a web service in my ASP.NET project. That web service is running a stored proc and packaging the data into an object to be returned to the client. When I look at the response via Firebug, everything is being sent back. However, when I try to access that response object it is null. Why?

My jQuery:

$(".listingManagerEdit").live("click", function () {
    $.post("/WebServices/AoP.asmx/GetListingManagerByID",
        JSON.stringify({
            ListingMgrID: $(this).closest("li.listingMgrs").data("listingmgrid")
        }),
        function (data) {
            $(".billingName").val(data.Name);
            $(".billingOrgName").val(data.OrgName);
            $(".billingAddress1").val(data.Address1);
            $(".billingAddress2").val(data.Address2);
            $(".billingCity").val(data.City);
            $(".billingState option:selected").val(data.State);
            $(".billingZipCode").val(data.Zip);
            $(".billingPhone").val(data.Phone);
            $(".billingPhoneExt").val(data.PhoneExt);
            $(".billingFax").val(data.Fax);
            $(".billingFaxExt").val(data.FaxExt);
            $(".billingEMail").val(data.Email);
        }
    );
});

My web service:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public ListingManager GetListingManagerByID(int ListingMgrID)
{
    ListingManager LM = new ListingManager();
    DataTable dt = DataAccess.AoP.GetListingManagerByID(ListingMgrID);

    LM.Name = dt.Rows[0]["Name"].ToString();
    LM.OrgName = dt.Rows[0]["OrgName"].ToString();
    LM.Address1 = dt.Rows[0]["Address1"].ToString();
    LM.Address2 = dt.Rows[0]["Address2"].ToString();
    LM.City = dt.Rows[0]["City"].ToString();
    LM.State = dt.Rows[0]["State"].ToString();
    LM.Zip = dt.Rows[0]["Zip"].ToString();
    LM.Phone = Utilities.FormatPhoneNumber(dt.Rows[0]["Phone"].ToString());
    LM.PhoneExt = dt.Rows[0]["PhoneExt"].ToString();
    LM.Fax = Utilities.FormatPhoneNumber(dt.Rows[0]["Fax"].ToString());
    LM.FaxExt = dt.Rows[0]["FaxExt"].ToString();
    LM.Email = dt.Rows[0]["Email"].ToString();

    return LM;
}

The response (according to FireBug):

d:  {…}
__type  BusinessLogic.AoP+ListingManager
Name:   John Smith
OrgName:    
Address1:   123 Pine Street, 3rd Floor
Address2:   
City:   San Francisco
State:  CA
Zip:    94104
Phone:  (415) 123-4567
PhoneExt:   
Fax:    
FaxExt: 
Email:  [email protected]

In the success function of my .post call, "data" is null.

Upvotes: 0

Views: 45

Answers (1)

wazz
wazz

Reputation: 5068

You are supposed to be passing back json but you're returning an object.

If you can use JSON.Net,

return JsonConvert.SerializeObject(LM);
// or serialize it in C#.

In the callback

function (data) {
    var o = JSON.parse(data);
    $(".billingName").val(data[0].Name);
    ...

Upvotes: 1

Related Questions