flynny
asked on
Ajax Return Issue
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.
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?
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"
});
}
}
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?
ASKER
Hi guru.
I did try adding this (although I would rather not set async false) but got the same behaviour. Do I not cover for this with my runajax method? As u thought I was implementing a promise with this code
I did try adding this (although I would rather not set async false) but got the same behaviour. Do I not cover for this with my runajax method? As u thought I was implementing a promise with this code
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi all,
thanks for the help. I have the following code;
now the first alert is returning Object object and the second alert is giving 1? From testing server side I am sending back
List<KeyValuePair<String, String>>
with 2 rows in it? Do I need to convert the return to an array?
thanks for the help. I have the following code;
function addFontField(element) {
runAjax('/Public/AdminServices.asmx/GetFonts', null)
.done(function (fonts) {
alert(fonts);
alert(Object.keys(fonts).length);
var html = "<ul class='fontList'>";
for(var i=0; i<fonts.length; i++)
{
html += "<li data-url='" + fonts[0][0] + "'>" + fonts[0][1] + "</li>";
}
html += "</ul>";
});
}
now the first alert is returning Object object and the second alert is giving 1? From testing server side I am sending back
List<KeyValuePair<String, String>>
with 2 rows in it? Do I need to convert the return to an array?
ASKER
Ok,
after googling I apologise i access the .d for return data;
However, I am facing a further issue. How do I access the string in the array as
is returning undefined for both elements?
after googling I apologise i access the .d for return data;
However, I am facing a further issue. How do I access the string in the array as
for(var i=0; i<fonts.d.length; i++)
{
html += "<li data-url='" + fonts.d[i][0] + "'>" + fonts.d[i][1] + "</li>";
}
is returning undefined for both elements?
Try this...
for(var i=0; i<fonts.d.length; i++)
{
html += "<li data-url='" + fonts.d[i]['Key'] + "'>" + fonts.d[i]['Value'] + "</li>";
}
You can test it by setting changing your async call to sync.
pass async:false to your .ajax method.
If you get the values with above setting, it means you will have to rethink your code flow...