javascript - How to do parallel Ajax request with jQuery for Posting and Commenting Mechanism -


i using asp.net mvc, trying make post , comment mechanism,i can comment , post , add them database in different tables, can take comment database index.cshtml page ajax request via jquery. have try take post , post's comment simultaneously, have created paralel() function given below not have idea how take comment , post simultaneously in function. me? thanks.

getposts():

function getposts() {         var tosend = new object();         if (timestamoflastpost == null) {             tosend.timestampfrom = 4294967295;//max_int         }         else {             tosend.timestampfrom = timestamoflastpost;         }         tosend.numberofposts = 2;          $.ajax({             url: '/home/getposts',             type: 'post',             contenttype: 'application/json',             data: json.stringify(tosend),             datatype: "json",             async:true,             success:function (data) {                 $.each(data.postlist, function (i, post) {                     postshtml += getmessagehtml(post.title, post.message, post.id, "", "");                      });                 $("#posts").html(postshtml);                 timestamoflastpost = data.timestamp;             },             error: function (jqxhr, exception) {                 alert('error message.');             }         });     } 

getcomment():

 function getcomment()     {         if (timestamoflastcomment == null) {             //tosend.timestampfrom = date.now() / 1000 | 0;             tosend.timestampfrom = 4294967295;//max_int         }         else {             tosend.timestampfrom = timestamoflastpost;         }         $.ajax({             url: '/home/getcomment',             type: 'post',             async:true,             contenttype: 'application/json',             data: json.stringify(tosend),             datatype: "json",             success: function (data) {    $.each(data.comment, function (i) {    comment += getcomment(comment.message,comment.author_id,comment.post_id)                               });           }              });     } 

paralel:

function paralel()         {             $.when($.ajax("/home/getposts"), $.ajax("/home/comment")).done(function(a1, a2)             {                ///to take post , comment              });          } 

getcommenthtml():

function getcommenthtml(message, author_id, post_id)     {         var result = '';         result += "<label>";         result += message;         result += "</label>";     } 

getmessagehtml():

function getmessagehtml(title, message,id,comment_id,comment_message) { var result = '';             result += "<div class=\"row col-md-8\">";             result += "<div class=\"row\">";             result += "<h2>" + title + "</h2>";             result += "</div>\n";             result += "<div class=\"row\">";             result += message;             result += "</div>\n";             result += "<hr width='%100'>";             result += "<div style='margin-left:100px' class=\"row\">";             result += "<hr width='%100'>";             result += "<form action='#' method='post'>";             result += "\n<textarea id='comment";             result += id             result += "'></textarea>\n";             result += "\n<input type='submit' class='btn btn-default' onclick='docomment("              result += ")' value='comment' />";             result += "</div>\n";             return result;         } 

if change paralel fucntion work:

function paralel() {    getposts();    getcomment() } 

your code run in "parallel" because $.ajax asynchronous.

in example, trying use promises $.when promises (getposts, getcomment) resolved. $.ajax prepared promised functions (getposts, getcomment) not. have use jquery promises (https://learn.jquery.com/code-organization/deferreds/examples/)


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -