Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1683
  • Last Modified:

How to return value from ajax success function?

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
duckyfuzz
Asked:
duckyfuzz
  • 3
  • 2
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
use :

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

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

or directly :

stamp =$($.ajax({
      url: 'php-assets/get-misc-results.php',
        async: false
}).responseText).find('stampDuty').text();
0
 
mickey159Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
duckyfuzzAuthor Commented:
That worked perfectly leakim, thank you!
0
 
duckyfuzzAuthor Commented:
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
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
 
mickey159Commented:
Oh sorry that I forget it.Thanks for reminding me.
But I am happy to see your problem solved.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now