How to ajax read external php file data string and then assign it to JavaScript variable

tantan6611
tantan6611 used Ask the Experts™
on
I'm trying to read data from external php file using ajax, then I want to assign this data to a javascript document variable. I want to check if my code is ok like that or should I define the variable inside the ajax brackets.

Here is my code:

    $.ajax({
      url: 'http://example.com/mydata.php',
      success: function(data) { alert(data); }  
       });
   
     var foo = data;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Anybody help please
Freelancer
Top Expert 2010
Commented:
Probably, something like this could work:
   var foo;
   $.ajax({
      url: 'http://example.com/mydata.php',
      success: function(data) { 
           alert(data); 
           foo = data;
        }  
  });

Open in new window

But if you can put here the php script, we can help you better. :-)
Most Valuable Expert 2011
Top Expert 2016

Commented:
... trying to read data from external php file using ajax, then I want to assign this data to a javascript document variable ...
This sounds a little confused.  Maybe if you explain the application in plain language (nothing technical at all) we can offer some solutions.

If you're looking for a simple jQuery / AJAX example, this is about as simple as it gets:
http://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html

Author

Commented:
Thanks alot, this is exactly what I was looking for.
Marco GasiFreelancer
Top Expert 2010

Commented:
You're welcome! Good luck with your project :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial