Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:40:40+00:00 2026-06-08T13:40:40+00:00

I need to load a php-script with ajax, which creates a JSON-result. An example

  • 0

I need to load a php-script with ajax, which creates a JSON-result.
An example of the getEvent.php is:

[{"id":"1","start":"Wed Jul 18 12:00:00 GMT+0200 2012","end":"Wed Jul 18 13:30:00 GMT+0200 2012","title":"leer"}]

In order to transmit this result to another function, I have to be able to assign it to an variable. I tried it many ways, but it has never worked out.

function loadEvents(){
   var cdata;

   $.getJSON({
     type: 'GET',
     url: 'getEvent.php',
     asynch:false,
     contentType: 'application/json; charset=utf-8',
     dataType: 'json',

     success: function(jsonData) {
        cdata = jsonData;
     },
     error: function() {
       alert('');
     }

   });
   return cdata;
 }

cdata = jsonData; doesnt seem to work

I have only found ways to asign parts of the result (jsonData) to a variable, but it seems that the entire jsonData can’t be returned.

Can anybody help me with my problem? I have to return the complete jsonData to another function…

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:40:41+00:00Added an answer on June 8, 2026 at 1:40 pm

    getJSON is an asynchronous function (expectiong just a url, no settings object (Docu)), meaning that it will return a result as soon as the answer from the server is done. In the meantime your function continues execution. Thus, at the moment you return cdata, your success function hasn’t executed yet. You need to either use a callback or set the function to be synchronous (which you are trying to do but have a typo in there – it’s async without h – also if you want to pass additional settings, you can’t use getJSON() but have to use $.ajax().

    instead of making the call synchronous, using the callback is probably the better solution:

    instead of

     success: function(jsonData) {
        cdata = jsonData;
     },
    

    write:

     success: function(jsonData) {
        workWithResult(jsonData);
     },
    
     // this is called the "callback" function, it is executed when the ajax-request
     // returned successfully
     function workWithResult(data){
         // now you can work with the data, which is guaranteed to exist
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need ajax to run a script of both php and javascript in the
i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
I need to load data into my treestore. My ajax request give me XML
I have a ajax image upload script which i found here http://www.fengcool.com/2009/06/ajax-form-upload-local-image-file-without-refresh/ The problem
My jQuery code (using ajax) request's data from a local php script (pgiproxy.php). This
I'm having some trouble with an Ajax script which disables my jQuery click events.
I am working on a comment script using ajax, json and jquery. I have
All my forms submit to a php script by Ajax. the php script just
I need to call an Ajax script to reload data from a child window.
I need help with jQuery and ajax. need to load textarea content from file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.