Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to return value from ajax success function?

Posted on 2010-11-15
7
Medium Priority
?
1,652 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
[X]
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
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

618 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