简体   繁体   中英

Ajax call returning string describing object instead of object

I'm running C# with MVC in ASP.NET, and I've got an ajax call that's supposed to return a list of objects, but instead it's just returning the string "System.Collections.Generic.List`1[Namespace.CustomObject]"

Clearly the data is making it back to the javascript, and returning List<int> doesn't change anything significant, so the object isn't at fault. Did I make a mistake in my ajax call that I've been missing, or do I need to use something other than list ?

My ajax call:

$.ajax({
        type: 'POST',
        url: url,
        data: arrayOfInts
        contentType: 'application/json',
        datatype: 'json',
        success: function (data) {
            if (data.length) {
                doThisIfDataReturned(data);
            } else {
                doThisIfNoDataReturned(productIds);
            }
        }
    });

And the method it calls:

public List<CustomObject> MakeAList(int[] productIds)
    {
        //
        // create objectList
        //
        return objectList; //debugger shows that list is correct here
    }

In the C# you need to return a JSON object instead of a List. I've done stuff like this in the past:

    public JsonResult myFunc()
    {
        ..... code here .....


        return Json(myList);
    }

Edit: Sometimes it is nice to see exactly what is being sent back before it is sent. One way to accomplish this is to assign the return object to a variable, then return the variable.

    public JsonResult myFunc()
    {
        ..... code here .....

        var x = Json(myList);
        return x;
    }

This does exactly the same thing but is slightly easier to debug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM