64603

Returning value from javascript object [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr" rel="nofollow">How do I return the response from an asynchronous call?</a> <span class="question-originals-answer-count"> 33 answers </span> </li> </ul>

I have created a javascript object and im trying to fetch som data from it. Im using jquery ajax to fetch send some values and then returing them from a php script. this works, but when i try to use an instanse of my javascript object to dispaly the value i get 'undefined'.

<strong>Object:</strong>

var getDBresults = (function () { function getResult(url,TableName ,callback){ $.ajax({ url: url, type: 'POST', data: { 'table':TableName, }, success: function(data){ callback(data); }, error: function(event){ alert(event.error); } }); } return { getAllVideoes: function(){ getResult("getAllResults.php", "videoer", function(data){ console.log(data); //Works return data; }); }, } })();

<strong>in my other script:</strong>

var obj = getDBresults; var data = obj.getAllVideoes(); //this runs, and produce the log showed above console.log(data) //results in undefined console.log(obj.getAllVideoes()) //results in undefined

Answer1:

Maybe you can use callbacks it will solve your async problems: <a href="http://recurial.com/programming/understanding-callback-functions-in-javascript/" rel="nofollow">http://recurial.com/programming/understanding-callback-functions-in-javascript/</a>

Recommend

  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Adjust width of select element according to selected option's width
  • Django rest serializer Breaks when data exists
  • How to install a .deb file on a jailbroken iphone programmatically?
  • DomPDF {PAGE_NUM} not on first page
  • Is there a javascript serializer for JSON.Net?
  • Javascript simulate pressing enter in input box
  • Array.prototype.includes - not transformed with babel
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Updating server-side rendering client-side
  • Fill an image in a square container while keeping aspect ratio
  • ILMerge & Keep Assembly Name
  • Where to put my custom functions in Wordpress?
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Importing jscolor library in angular 2
  • How to limit post in wp_query
  • Upload files with Ajax and Jquery
  • Websockets service method fails during R startup
  • Large data - storage and query
  • Do I've to free mysql result after storing it?
  • WOWZA + RTMP + HTML5 Playback?
  • How to pass list parameters for each object using Spring MVC?
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • A cron job substitute?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • How to set the response of a form post action to a iframe source?
  • Understanding cpu registers
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • sending mail using smtp is too slow
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?