returning int in ajax complete
11,140
Solution 1
The data
that you are returning in your controller actually winds up being a property on the data
in your javascript.
This should give you the value:
var id = parseInt(data.data);
Solution 2
When you write
return Json(new { data = Model.UserID });
You are returning one object to the success call of ajax. You can wrap multiple properties inside this object e.g
return Json(new { data = Model.UserID,Name=Model.Name,AnyProp=Model.SomeProp });
You should access this in success function and not in complete as complete is called even if error occurs
success: function (data) {
var id = parseInt(data.data);
var name=data.Name;
var prop=data.AnyProp;
window.location.url = '/Upload/Myuploads/UserID=' + id;
}
Comments
-
user3163213 almost 2 years
I am returing integer in Json result and then want to deal with that result.
Returning it like -
return Json(new { data = Model.UserID });
Trying to use it in ajaxComplete function-
complete: function (data) { var id = parseInt(data); window.location.url = '/Upload/Myuploads/UserID=' + id; }
But this function shows me null at the end point.
How do I deal with integer results in ajax
-
user3163213 about 10 yearsVapre, For instance this property of id is happening on onComplete not on
success
function. My wild guess is it doesn't even know what the data is. -
user3163213 about 10 yearsFor instance this property of id is happening on onComplete not on
success
function. My wild guess is it doesn't even know what the data is. I think there must be some difference on complete and success results of ajax. Right? -
Nitin Varpe about 10 yearsits Varpe, and Complete will always be called after success. Test it from ur side
-
user3163213 about 10 yearsYou are right. But I don't know why things are not happening even after following it in right way today for me.
-
krillgar about 10 yearsThat could be another issue. I didn't catch that when I read your question. I would change to success as my preference. But open the Javascript Console (Ctrl-Shift-J) and put a breakpoint inside your complete function and take a look at your
data
object. The point still stands though that you have some confusion because you're using the same name for two different objects. -
user3163213 about 10 yearsThat was the reason, changed it to success as you preferred. I still can't understand for complete function. Finally, Got it working with changing
complete
tosuccess
. -
krillgar about 10 years
complete
would be for something to do regardless of success or failure. But it wouldn't have access to anything inside the scope of either thesuccess
orfailure
methods as it is a different method call together.