Solved

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

Posted on 2011-02-19
6
329 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery Parallax 1 38
Facebook React and SyntheticEvents 6 46
Add Context Menu Item 4 43
Javascript: How to show value of a specific value 5 44
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 …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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)

932 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

15 Experts available now in Live!

Get 1:1 Help Now