How to return javascript success data to jQuery property [duplicate]












0
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




The below function is working fine and I am getting response from ajax get function, the data is comming in 'res.data.value' in JSON format, Now i want to pass this data to events property of full calender in array format. If i am passing static values to events property it is working fine. Please help me as I am unable to pass 'res.data.value' value dynamically.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

var res = makeRequest(requestParams);
res
.done(function(res) {
return [res.data.value];
})
.fail(function(err) {
console.log(err);
});

};

$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: completedInterviews(), // I want [res.data.value] value here.
loading: function(bool) {
$('#loading').toggle(bool);
}
});

});

</script>


/*
* function to make API call using AJAX, Deffered and Promise
*/



function makeRequest(requestParams) {
var headers = {
'Authorization': 'Bearer ' + authToken
};
// concating extra headers
headers = jsonConcat(headers, requestParams.headers);

// checking return content type
var dataType = "json";
if(headers.Accept == "application/xml") {
dataType = "xml";
}

var deferred = $.Deferred();

var response = $.ajax({
url: requestParams.url,
type: requestParams.requestType,
dataType: dataType,
data: JSON.stringify(requestParams.params),
headers: headers,
success: function(res) {
if(res.error != null) {
deferred.reject(res.error);
if(res.error=="Invalid Token") {
//window.location.href = "";
}
} else {
deferred.resolve(res);
}
},
error: function(err) {
deferred.reject(err);
}
});

return deferred.promise();
}









share|improve this question















marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • try JSON.parse(res.data.value)

    – Elyas Esna
    Nov 24 '18 at 6:57











  • Thanks for reply, .done(function(res) { return JSON.parse(res.data.value); }) is not working

    – wasim beg
    Nov 24 '18 at 7:16











  • Avoid the deferred antipattern!

    – Bergi
    Nov 24 '18 at 18:40
















0
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




The below function is working fine and I am getting response from ajax get function, the data is comming in 'res.data.value' in JSON format, Now i want to pass this data to events property of full calender in array format. If i am passing static values to events property it is working fine. Please help me as I am unable to pass 'res.data.value' value dynamically.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

var res = makeRequest(requestParams);
res
.done(function(res) {
return [res.data.value];
})
.fail(function(err) {
console.log(err);
});

};

$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: completedInterviews(), // I want [res.data.value] value here.
loading: function(bool) {
$('#loading').toggle(bool);
}
});

});

</script>


/*
* function to make API call using AJAX, Deffered and Promise
*/



function makeRequest(requestParams) {
var headers = {
'Authorization': 'Bearer ' + authToken
};
// concating extra headers
headers = jsonConcat(headers, requestParams.headers);

// checking return content type
var dataType = "json";
if(headers.Accept == "application/xml") {
dataType = "xml";
}

var deferred = $.Deferred();

var response = $.ajax({
url: requestParams.url,
type: requestParams.requestType,
dataType: dataType,
data: JSON.stringify(requestParams.params),
headers: headers,
success: function(res) {
if(res.error != null) {
deferred.reject(res.error);
if(res.error=="Invalid Token") {
//window.location.href = "";
}
} else {
deferred.resolve(res);
}
},
error: function(err) {
deferred.reject(err);
}
});

return deferred.promise();
}









share|improve this question















marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • try JSON.parse(res.data.value)

    – Elyas Esna
    Nov 24 '18 at 6:57











  • Thanks for reply, .done(function(res) { return JSON.parse(res.data.value); }) is not working

    – wasim beg
    Nov 24 '18 at 7:16











  • Avoid the deferred antipattern!

    – Bergi
    Nov 24 '18 at 18:40














0












0








0









This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




The below function is working fine and I am getting response from ajax get function, the data is comming in 'res.data.value' in JSON format, Now i want to pass this data to events property of full calender in array format. If i am passing static values to events property it is working fine. Please help me as I am unable to pass 'res.data.value' value dynamically.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

var res = makeRequest(requestParams);
res
.done(function(res) {
return [res.data.value];
})
.fail(function(err) {
console.log(err);
});

};

$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: completedInterviews(), // I want [res.data.value] value here.
loading: function(bool) {
$('#loading').toggle(bool);
}
});

});

</script>


/*
* function to make API call using AJAX, Deffered and Promise
*/



function makeRequest(requestParams) {
var headers = {
'Authorization': 'Bearer ' + authToken
};
// concating extra headers
headers = jsonConcat(headers, requestParams.headers);

// checking return content type
var dataType = "json";
if(headers.Accept == "application/xml") {
dataType = "xml";
}

var deferred = $.Deferred();

var response = $.ajax({
url: requestParams.url,
type: requestParams.requestType,
dataType: dataType,
data: JSON.stringify(requestParams.params),
headers: headers,
success: function(res) {
if(res.error != null) {
deferred.reject(res.error);
if(res.error=="Invalid Token") {
//window.location.href = "";
}
} else {
deferred.resolve(res);
}
},
error: function(err) {
deferred.reject(err);
}
});

return deferred.promise();
}









share|improve this question

















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




The below function is working fine and I am getting response from ajax get function, the data is comming in 'res.data.value' in JSON format, Now i want to pass this data to events property of full calender in array format. If i am passing static values to events property it is working fine. Please help me as I am unable to pass 'res.data.value' value dynamically.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

var res = makeRequest(requestParams);
res
.done(function(res) {
return [res.data.value];
})
.fail(function(err) {
console.log(err);
});

};

$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: completedInterviews(), // I want [res.data.value] value here.
loading: function(bool) {
$('#loading').toggle(bool);
}
});

});

</script>


/*
* function to make API call using AJAX, Deffered and Promise
*/



function makeRequest(requestParams) {
var headers = {
'Authorization': 'Bearer ' + authToken
};
// concating extra headers
headers = jsonConcat(headers, requestParams.headers);

// checking return content type
var dataType = "json";
if(headers.Accept == "application/xml") {
dataType = "xml";
}

var deferred = $.Deferred();

var response = $.ajax({
url: requestParams.url,
type: requestParams.requestType,
dataType: dataType,
data: JSON.stringify(requestParams.params),
headers: headers,
success: function(res) {
if(res.error != null) {
deferred.reject(res.error);
if(res.error=="Invalid Token") {
//window.location.href = "";
}
} else {
deferred.resolve(res);
}
},
error: function(err) {
deferred.reject(err);
}
});

return deferred.promise();
}




This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers








javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 21:40









marc_s

578k12911171263




578k12911171263










asked Nov 24 '18 at 6:53









wasim begwasim beg

308




308




marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • try JSON.parse(res.data.value)

    – Elyas Esna
    Nov 24 '18 at 6:57











  • Thanks for reply, .done(function(res) { return JSON.parse(res.data.value); }) is not working

    – wasim beg
    Nov 24 '18 at 7:16











  • Avoid the deferred antipattern!

    – Bergi
    Nov 24 '18 at 18:40



















  • try JSON.parse(res.data.value)

    – Elyas Esna
    Nov 24 '18 at 6:57











  • Thanks for reply, .done(function(res) { return JSON.parse(res.data.value); }) is not working

    – wasim beg
    Nov 24 '18 at 7:16











  • Avoid the deferred antipattern!

    – Bergi
    Nov 24 '18 at 18:40

















try JSON.parse(res.data.value)

– Elyas Esna
Nov 24 '18 at 6:57





try JSON.parse(res.data.value)

– Elyas Esna
Nov 24 '18 at 6:57













Thanks for reply, .done(function(res) { return JSON.parse(res.data.value); }) is not working

– wasim beg
Nov 24 '18 at 7:16





Thanks for reply, .done(function(res) { return JSON.parse(res.data.value); }) is not working

– wasim beg
Nov 24 '18 at 7:16













Avoid the deferred antipattern!

– Bergi
Nov 24 '18 at 18:40





Avoid the deferred antipattern!

– Bergi
Nov 24 '18 at 18:40












1 Answer
1






active

oldest

votes


















0














Use calendar in then statement.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

res = makeRequest(requestParams);

//RETURN RES
return res;

};

//WRAP CALENDAR WITH THEN
completedInterviews().then(function(data){
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: data, // ADD DATA.
loading: function(bool) {
$('#loading').toggle(bool);
}
});
});



});

</script>





share|improve this answer


























  • I edit my code, still didn't get success , if i console.log(completedInterviews()) , it gives me undefined

    – wasim beg
    Nov 24 '18 at 13:09











  • see my edited code

    – wasim beg
    Nov 24 '18 at 13:31











  • @wasimbeg edited answer. can you try again with that solition?

    – akcoban
    Nov 24 '18 at 16:15











  • Thanks for your efforts, when I do console.log(data) , it gives me res value i.e. {state: ƒ, always: ƒ, catch: ƒ, pipe: ƒ, then: ƒ, …} rather than [res.data.value];

    – wasim beg
    Nov 24 '18 at 18:25











  • Can you delete done and fail from res. Return res directly.

    – akcoban
    Nov 24 '18 at 18:30




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Use calendar in then statement.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

res = makeRequest(requestParams);

//RETURN RES
return res;

};

//WRAP CALENDAR WITH THEN
completedInterviews().then(function(data){
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: data, // ADD DATA.
loading: function(bool) {
$('#loading').toggle(bool);
}
});
});



});

</script>





share|improve this answer


























  • I edit my code, still didn't get success , if i console.log(completedInterviews()) , it gives me undefined

    – wasim beg
    Nov 24 '18 at 13:09











  • see my edited code

    – wasim beg
    Nov 24 '18 at 13:31











  • @wasimbeg edited answer. can you try again with that solition?

    – akcoban
    Nov 24 '18 at 16:15











  • Thanks for your efforts, when I do console.log(data) , it gives me res value i.e. {state: ƒ, always: ƒ, catch: ƒ, pipe: ƒ, then: ƒ, …} rather than [res.data.value];

    – wasim beg
    Nov 24 '18 at 18:25











  • Can you delete done and fail from res. Return res directly.

    – akcoban
    Nov 24 '18 at 18:30


















0














Use calendar in then statement.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

res = makeRequest(requestParams);

//RETURN RES
return res;

};

//WRAP CALENDAR WITH THEN
completedInterviews().then(function(data){
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: data, // ADD DATA.
loading: function(bool) {
$('#loading').toggle(bool);
}
});
});



});

</script>





share|improve this answer


























  • I edit my code, still didn't get success , if i console.log(completedInterviews()) , it gives me undefined

    – wasim beg
    Nov 24 '18 at 13:09











  • see my edited code

    – wasim beg
    Nov 24 '18 at 13:31











  • @wasimbeg edited answer. can you try again with that solition?

    – akcoban
    Nov 24 '18 at 16:15











  • Thanks for your efforts, when I do console.log(data) , it gives me res value i.e. {state: ƒ, always: ƒ, catch: ƒ, pipe: ƒ, then: ƒ, …} rather than [res.data.value];

    – wasim beg
    Nov 24 '18 at 18:25











  • Can you delete done and fail from res. Return res directly.

    – akcoban
    Nov 24 '18 at 18:30
















0












0








0







Use calendar in then statement.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

res = makeRequest(requestParams);

//RETURN RES
return res;

};

//WRAP CALENDAR WITH THEN
completedInterviews().then(function(data){
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: data, // ADD DATA.
loading: function(bool) {
$('#loading').toggle(bool);
}
});
});



});

</script>





share|improve this answer















Use calendar in then statement.



<script>

$(document).ready(function() {

var completedInterviews = function() {
var requestParams = ;
requestParams = {
"url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
"requestType": "GET",
"headers": {
"Accept": "application/json"
}
}

res = makeRequest(requestParams);

//RETURN RES
return res;

};

//WRAP CALENDAR WITH THEN
completedInterviews().then(function(data){
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay,listWeek'
},
defaultDate: '2018-03-12',
editable: true,
navLinks: true,
eventLimit: true,
events: data, // ADD DATA.
loading: function(bool) {
$('#loading').toggle(bool);
}
});
});



});

</script>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 18:33

























answered Nov 24 '18 at 10:11









akcobanakcoban

42119




42119













  • I edit my code, still didn't get success , if i console.log(completedInterviews()) , it gives me undefined

    – wasim beg
    Nov 24 '18 at 13:09











  • see my edited code

    – wasim beg
    Nov 24 '18 at 13:31











  • @wasimbeg edited answer. can you try again with that solition?

    – akcoban
    Nov 24 '18 at 16:15











  • Thanks for your efforts, when I do console.log(data) , it gives me res value i.e. {state: ƒ, always: ƒ, catch: ƒ, pipe: ƒ, then: ƒ, …} rather than [res.data.value];

    – wasim beg
    Nov 24 '18 at 18:25











  • Can you delete done and fail from res. Return res directly.

    – akcoban
    Nov 24 '18 at 18:30





















  • I edit my code, still didn't get success , if i console.log(completedInterviews()) , it gives me undefined

    – wasim beg
    Nov 24 '18 at 13:09











  • see my edited code

    – wasim beg
    Nov 24 '18 at 13:31











  • @wasimbeg edited answer. can you try again with that solition?

    – akcoban
    Nov 24 '18 at 16:15











  • Thanks for your efforts, when I do console.log(data) , it gives me res value i.e. {state: ƒ, always: ƒ, catch: ƒ, pipe: ƒ, then: ƒ, …} rather than [res.data.value];

    – wasim beg
    Nov 24 '18 at 18:25











  • Can you delete done and fail from res. Return res directly.

    – akcoban
    Nov 24 '18 at 18:30



















I edit my code, still didn't get success , if i console.log(completedInterviews()) , it gives me undefined

– wasim beg
Nov 24 '18 at 13:09





I edit my code, still didn't get success , if i console.log(completedInterviews()) , it gives me undefined

– wasim beg
Nov 24 '18 at 13:09













see my edited code

– wasim beg
Nov 24 '18 at 13:31





see my edited code

– wasim beg
Nov 24 '18 at 13:31













@wasimbeg edited answer. can you try again with that solition?

– akcoban
Nov 24 '18 at 16:15





@wasimbeg edited answer. can you try again with that solition?

– akcoban
Nov 24 '18 at 16:15













Thanks for your efforts, when I do console.log(data) , it gives me res value i.e. {state: ƒ, always: ƒ, catch: ƒ, pipe: ƒ, then: ƒ, …} rather than [res.data.value];

– wasim beg
Nov 24 '18 at 18:25





Thanks for your efforts, when I do console.log(data) , it gives me res value i.e. {state: ƒ, always: ƒ, catch: ƒ, pipe: ƒ, then: ƒ, …} rather than [res.data.value];

– wasim beg
Nov 24 '18 at 18:25













Can you delete done and fail from res. Return res directly.

– akcoban
Nov 24 '18 at 18:30







Can you delete done and fail from res. Return res directly.

– akcoban
Nov 24 '18 at 18:30







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga