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

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;
tantan6611Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tantan6611Author Commented:
Anybody help please
Marco GasiFreelancerCommented:
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. :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
... 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
tantan6611Author Commented:
Thanks alot, this is exactly what I was looking for.
Marco GasiFreelancerCommented:
You're welcome! Good luck with your project :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.