?
Solved

How to return value from ajax success function?

Posted on 2010-11-15
7
Medium Priority
?
1,646 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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

777 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