We help IT Professionals succeed at work.
Get Started

Ajax Return Issue

flynny
flynny asked
on
702 Views
Last Modified: 2014-08-20
Hi all,

I am trying to return values from an ajax return but am having difficulty, I assume I am missing something regarding the .promise or async of the method.

function addFontField(element) {
    var fonts = runAjax('/Public/AdminServices.asmx/GetFonts', null);
    alert("fonts" + fonts); //is alerting undefined

}

function getFonts() {

    runAjax('/Public/AdminServices.asmx/GetFonts', null)
        .done(
            function (__exists) {

                if (__exists.d) {
                    alert(__exists.d); // alerts the [Object object, Object object] array as expected
                    return __exists.d;
                }
                else {
                   alert(__exists.d);
                   return false;
                }
            }
        )
        .fail(
            function (jqXHR, textStatus, errorThrown) {
              alert(errorThrown + ':' + textStatus + ':' + jqXHR.responseText);
              return false;
            }
        );

}

function runAjax(webmethod, fields) {

    //stringify passed field array
    if (fields !== null) {
        var jsonObject = {};

        for (var i = 0; i < fields.length; i++) {
            jsonObject[fields[i].fieldname] = fields[i].fieldtext;
        }

        return $.ajax({
            type: "POST",
            url: webmethod,
            data: JSON.stringify(jsonObject),
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    }
    else {
        return $.ajax({
            type: "POST",
            url: webmethod,
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    }  
}

Open in new window


Now I am returning a List<KeyValuePair<String, String>> from my c# function. It appears to be getting to the .success method, but when i return it to the var in getfonts() it is undefined at this point?
Comment
Watch Question
Owner (Aidellio)
CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
This problem has been solved!
Unlock 3 Answers and 8 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE