0

My variable data in function ShowFavorits is undefined even do that my ajax call do return a json string.

<script type="text/javascript">
$(document).ready(function () {
    ShowFavorits();

    function AjaxGet() {
        var param = "{'_userID': '1337'}";
        $.ajax({
            type: "POST",
            url: "/webservices/MinSide.asmx/GetFavorits",
            data: param,
            contentType: "application/json;",
            dataType: "json",
            success: function (data) {
                if (data.hasOwnProperty("d")) {                      
                    return (data.d);
                }
            },
            error: function (data) {
                //error
            }
        });
    }

    function ShowFavorits() {
        var data = AjaxGet();

        $("#addedList").html(
        $("#addedTemplate").render(data)
        );
    }
});

        [WebMethod]
    public string GetFavorits(string _userID)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        jss.MaxJsonLength = int.MaxValue;
        string JsonData = string.Empty;

        var db = new ModelDataContext();
        var list = db.table.Where(x => x.userID == _userID).OrderBy(x=> x.TimePin).ToList();

        JsonData = jss.Serialize(list);  
        return (JsonData);
    }

Why cant i return the result from my ajax?

Hope someone can help me, have been stuck for hours now debugging this.

Thanks in advance.

1

1 Answer 1

1

The call to $.ajax in AjaxGet is asynchronous: the function returns undefined because the ajax call hasn't finished.

You should move the call to ShowFavourits into the ajax success function so that it executes once the ajax call is complete/successful

<script type="text/javascript">
$(document).ready(function () {
    // Kick-off the ajax request
    AjaxGet();

    function AjaxGet() {
        var param = {_userID: '1337'};
        $.ajax({
            type: "POST",
            url: "/webservices/MinSide.asmx/GetFavorits",
            data: param,
            dataType: "json",
            success: function (data) {
                if (data.hasOwnProperty("d")) {                      
                    ShowFavorits(data.d); // Pass the data to the template
                }
            }
        });
    }

    function ShowFavorits(data) {
        $("#addedList").html(
            $("#addedTemplate").render(data)
        );
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for your help. I now get the data to ShowFavorits() but its not working. If i hardcode the JSON from AjaxGet into ex var data2 and pass that to render then it works.
Then you need to focus on debugging the GetFavorits WebMethod: step it through a debugger and check that: you're getting data, it's being serialised properly, that the response is being sent correctly (right mime type, no extra data in response). Also, you don't need to set contentType in ajax request. And turn that param into a proper object rather than a string e.g. {_userID: '1337'} (unless your method accepts and deserialises JSON strings - not clear from code sample)
If i return a List from my WebMethod instead of a string it works fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.