Solved

How to return value from ajax success function?

Posted on 2010-11-15
7
1,626 Views
Last Modified: 2012-05-10
This alerts me with the value I want:
$.ajax({
	url: 'php-assets/get-misc-results.php',
	success: function(xml){
	    alert($(xml).find('stampDuty').text());
	}
});

Open in new window


How can I return  the value so I can use it outside the success function?

This seems to  point to the ajax object rather than the value:

stamp =  $.ajax({
	url: 'php-assets/get-misc-results.php',
	success: function(xml){
		return $(xml).find('stampDuty').text();
	}
});

Open in new window

0
Comment
Question by:duckyfuzz
  • 3
  • 2
  • 2
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34140799
use :

stamp = $.ajax({
      url: 'php-assets/get-misc-results.php',
        async: false
}).responseText;
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34140824
then :

stamp = $(stamp).find('stampDuty').text();

or directly :

stamp =$($.ajax({
      url: 'php-assets/get-misc-results.php',
        async: false
}).responseText).find('stampDuty').text();
0
 
LVL 6

Expert Comment

by:mickey159
ID: 34143497
You want to insert the ajax value to stamp?
so do something like this:
$.ajax({
url: 'php-assets/get-misc-results.php',
async: false
success: function(data){
var stamp=$(data).find('stampDuty').text());
}
});

Good luck!
0
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.

 

Author Closing Comment

by:duckyfuzz
ID: 34148986
That worked perfectly leakim, thank you!
0
 

Author Comment

by:duckyfuzz
ID: 34149009
mickey,

while that does allow me to access the value I want. I believe it's a local variable of the success function. I was looking to have it returned so I can use it outside the function.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34149035
You're welcome! Thanks for the points!
0
 
LVL 6

Expert Comment

by:mickey159
ID: 34153601
Oh sorry that I forget it.Thanks for reminding me.
But I am happy to see your problem solved.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
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…

775 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