jquery and javascript
jquery and javascript
They don't seem to be playing nice together, or I'm doing something wrong..?? This function keeps returning "undefined" when I can see the json data correctly. At first I thought it's a global var issue with javascript, but I think I'm declaring "rPiece" properly. Do I have to send "rPiece" to and from the XHR request? I also tried setting "rPiece" outside the function with no luck. Please help. Thank you.
Here's the function:
- function fnFormatDetails(){
- var rPiece;
-
- $.post("../i/php/autos-exe.php",
- {pieceid:"12345"},
- function(data){
- if(data.pieces.length>0){
- rPiece = '<table>';
- for (var i=0;i<data.pieces.length;i++){
- rPiece += '<tr><td>'+data.pieces[i].id+'</td></tr>';
- }
- rPiece += '</table>';
- }
- }
- ,"json");
-
- return rPiece;
- }
Here's the returned JSON data:
{"pieces":[{"id":"8653"}]}