Solved

jQuery .POST not posting results on html

Posted on 2011-02-19
10
375 Views
Last Modified: 2012-05-11
I don't see what I am doing wrong.

Here is the code
$("#EntireMenu a").live('click',function(){
					
				//$('.divPage:last').remove();
					var myObj = $(this);
				
				switch ($(this).attr('class')){
					case 'mReports':
						$('.divPage:first').fadeOut(50, function(){
								$(this).remove();
						});
						$("<div/>", {
				      "id": "RepPage",
				      "class":"divPage1",
															}).hide().appendTo("#body"); 
						$.ajax({
										type:"POST",
										url:'/aaMain/reports.php',
										data:{repType: myObj.attr('href').split('#')[1]
											},
										dataType:'json',
										success:function(responseText){
												// handle response
												alert('we got somethn!');
												$('#RepPage').html(responseText, function(){
														$('#RepPage').show();
													});
											}
									});
						
						//alert ('we got class - mReports!');
						var width_div = $('.divPage table').width() ? $('.divPage table').width() :1200;
						
		    		$('#footer').css({'position':'absolute','top':'500', 'width':width_div});
		      	$('#footer').fadeIn();
		      	// get data
		      	
						return false;
						break;

Open in new window


the switch continues.... but the code that is not working is what is above.

In firebug I can see more of what is going on , but I still don't understand why it shows the html in firebug but not on my page see attached image.
jQuery-POST.png
0
Comment
Question by:gbeaulac
  • 6
  • 4
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34935508
Try an synchrone ajax call, so replace your current ajax call by the following :


var rt = $.ajax({type:"POST",url:'/aaMain/reports.php',data:{repType: myObj.attr('href').split('#')[1]},dataType:'json',"async":false}).responseText;
					alert('we got somethn!');
					$('#RepPage').html(rt, function() { $('#RepPage').show(); });

Open in new window

0
 

Author Comment

by:gbeaulac
ID: 34935575
It spits out the alert, but still does not show the html.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34935584
do you get your html code in the alert with the following ?
var rt = $.ajax({type:"POST",url:'/aaMain/reports.php',data:{repType: myObj.attr('href').split('#')[1]},dataType:'json',"async":false}).responseText;
alert(rt);
$('#RepPage').html(rt);
$('#RepPage').show();

Open in new window

0
 

Author Comment

by:gbeaulac
ID: 34935595
YES!

I removed the alert and now we have display!

But can you also tell me what was wrong with my code? Why it would not work?
0
 

Author Closing Comment

by:gbeaulac
ID: 34935600
This is the second time you have helped me. I am grateful.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gbeaulac
ID: 34935606
When I make a second choice from my menu, it does not display until I reclick the menu. Any reason for that?
0
 

Author Comment

by:gbeaulac
ID: 34935618

I was reading your code and just clued in (even though you mentioned it in your first answer) that we are not going async. Isn't that defeating the purpose of using ajax?
I am sure you have a good reason, don't get me wrong, but I was wondering if you could explain why that is, please?

Sorry about the tons of questions...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34936966
>When I make a second choice from my menu, it does not display until I reclick the menu. Any reason for that?
>I was reading your code and just clued in (even though you mentioned it in your first answer) that we are not going async. Isn't that defeating the purpose of using ajax?

I was thinking we need to wait the data before continue the switch
With an async ajax call the program don't wait the end of the call and continue. So if you're doing thing thinking the data is available, it's not the case, for example moving a div of changing style attibute of object not available (before the end of the ajax call)

generaly you can use a async ajax call (generaly) but you need to have control of all what happen in the code.
0
 

Author Comment

by:gbeaulac
ID: 34937819
ok Thank you
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34937821
np, have a nice sunday !
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now