Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Why does this $.ajax work and not the other?

Posted on 2011-02-19
6
334 Views
Last Modified: 2012-05-11
Why does this code not work

$.ajax({
			type:"POST",
			url:'/aaMain/reports.php',
			data:{repType: myObj.attr('href').split('#')[1]
		},
			dataType:'json',
		success:function(responseText){
		// handle response
		$('#RepPage').html(responseText, function(){
		$('#RepPage').show();
		});
	}
});

Open in new window



BUT this code works?

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

Open in new window

0
Comment
Question by:gbeaulac
6 Comments
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34935908
In the first, add a hook to the error function and see if you are getting any errors.
0
 
LVL 4

Accepted Solution

by:
LAMASE earned 250 total points
ID: 34936594
Try with

async:false

Open in new window


without quotes


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

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 34936985
http://javascript.about.com/od/ajax/a/ajaxasyn.htm

the first one don't wait for the data and execture the lines of code below your ajax call (line 14 and so on...)
the second wait for the data before executing the lines of code below your ajax call  (line 6,7, and so on...)

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Expert Comment

by:LAMASE
ID: 34937450
Sorry for my previous post, i didn't understand wich one was not working:

in jQuery documentation i can't see any second parameter for html()

$('#RepPage').html(responseText, function(){
    $('#RepPage').show();
});

Open in new window


so probably this will work

$('#RepPage').html(responseText);
$('#RepPage').show();

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34937675
"async":false
is correcter than
async:false

and I am sure you can do

$('#RepPage').html(responseText).show();
0
 

Author Closing Comment

by:gbeaulac
ID: 34937947
Thank you all.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

840 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