Problem:
When ASP. NET is used in combination with JQuery + Ashx to load the message system, extract the Message ID from the message data read by PAGE and then query the reply content corresponding to the message using Ajax. The Code is as follows:
Copy to ClipboardReference: [www.bkjia.com] var str;/* load the message */
Var loadguest = function (){
$. Ajax ({
Url: '/Ashx/GetMsgs. ashx ',
Type: "GET ",
Data: {page: page}, // page number
DataType: 'json ',
BeforeSend: function (){
},
Error: function (){
;
},
Success: function (msg ){
$ ("# Contact_msg"). empty ();
If (msg! = "0 "){
Var data = msg. table;
Str = "";
$. Each (data, function (I, n ){
Str + = "<table width = '000000' border = '1' cellpadding = '0' cellspacing = '0' class = 'msg _ table'> ";
Str + = "<tr> ";
Str + = "<td width = '000000' rowspan = '3' align = 'center' valign = 'top' class = 'tr _ img '> <p class = 'msg _ id'> ID: "+ n. ID + "</p> ";
Str + = " <br/> ";
Str + = "<span class = 'msg _ name'>" + n. CustName + "</span> <br/> ";
Str + = "<span class = 'msg _ ip'>" + n. ip + "</span> </td> ";
Str + = "<td width = '000000' height = '20' valign = 'middle'> <span class = 'msg _ title'>" + n. title + "</span> <span class = 'msg _ date'>" + n. dt + "</span> </td> ";
Str + = "</tr> <tr> ";
Str + = "<td height = '000000' valign = 'top' class = 'msg 'id = 'msg _ id" + n. ID + "'> <p>" + n. content + "</p> ";
GetMsgRly (n. ID); // query the corresponding response using the ID of the message record to read and write str.
Str + = "<div id = 'msg _ rly" + n. ID + "'class = 'rly '> <textarea name = 'tarea' id = 'textarea" + n. ID + "'cols = '45' rows = '5'> </textarea> ";
Str + = "<a href = '; 'title =' click to reply 'onlick = 'submit (" + n. ID + ") '> reply </a> <a href ='; 'title = 'cancel reply 'onclick = 'cancelrly (" + n. ID + ") 'id = 'cancel" + n. ID + "'> cancel </a> </div> </td> ";
Str + = "<tr> <td height = '20' valign = 'middle'> <a href = '# 'title =' click to chat with me 'class = 'msg _ a'> </a> <a href =' # 'title = 'click to send me an email 'class = 'msg _'> </a> <a href =' # 'title = 'click to access my website' class = 'msg _ a'> </a> <span class = 'msg _ manager'> <a name =' "+ n. ID + "'href = '; 'title =' click to reply to this message 'onclick = 'showrly (" + n. ID + ") 'class = 'msg _ a'> </a> <a name ='" + n. ID + "'href = '; 'title =' click to delete the message 'onclick = 'deletemsg (" + n. ID + ") 'class = 'msg _ a'> </a> </span> </td> ";
Str + = "</table> ";
});
$ ("# Contact_msg"). append (str );
}
Else {
;
}
}
})
}
/* Load the message reply content */
Var GetMsgRly = function (MsgID ){
$. Ajax ({
Url: '/Ashx/GetMsgRly. ashx ',
Type: "GET ",
Data: {MsgID: MsgID },
DataType: 'json ',
BeforeSend: function (){
},
Error: function (){
;
},
Success: function (rly ){
If (rly! = "0 "){
Var rlydata = rly. table;
Rlystr = "";
$. Each (rlydata, function (I, r ){
/* The str operation will be lost after the str operation is completed. However, the corresponding content can be read and written, and the content can be displayed using alert */
Str + = "<span class = 'msg _ rly '> <span class = 'msg _ rly_line'> </span> ";
Str + = "<span class = 'msg _ rly_manager '> administrator reply: </span> <br/> ";
Str + = "<span class = 'msg _ rly_content '>" + r. Content + "</span> ";
Str + = "<span class = 'msg _ rly_Dt '> in" + r. Dt + "</span> ";
});
}
}
});
}
The result is: the message record can be read, but the corresponding message reply content cannot be read!
PS: the message table and the message reply table have different tables, which are associated by the primary key ID of the message table.
Do not question my background code. The message and message content can be read, but it is invalid for str reading and writing.
For help, why? Is there any solution?
Answer:
The code is executed here. GetMsgRly (n. ID) will not wait until the result is returned before executing the function.
Because GetMsgRly (n. ID) is asynchronous
Put GetMsgRly (n. ID) in $ ("# contact_msg"). append (str); and then execute
Then, append the content in GetMsgRly (n. ID) to the specified html container.