Solved

How to return value from ajax success function?

Posted on 2010-11-15
7
1,621 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
Comment Utility
use :

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

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Closing Comment

by:duckyfuzz
Comment Utility
That worked perfectly leakim, thank you!
0
 

Author Comment

by:duckyfuzz
Comment Utility
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
Comment Utility
You're welcome! Thanks for the points!
0
 
LVL 6

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jQuery Conflict 27 46
C3-->D3 Line Chart 4 16
jQuery Canlendar click function 5 19
gif overlay 3 32
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now