7

Trying to put together all the code of my ajax request into a function so I do not repeat myself. The code works perfectly outside but not in the function. Well fuck JavaScript. I've been stuck here for two days.

Comments
  • 1
    think asynchrone dude
  • 0
    *brainstorming* What arguments do you pass through to the function, do they contain all the data you need and in the correct format? Do you pass through mutable objects? Are you attempting to return something from the function (wrong) or do you use callbacks (right)? Have you accidentally declared some variable(s) as global or local?

    These are some ideas, not sure if this helps.
  • 1
    Javascript scopes are bitches, all of them. Try using arrow functions instead of the function keyword? Look them up if you don't know them yet, the difference is how they handle scope, which is probably linked to your issue
  • 0
    @theScientist the original code
  • 1
    @theScientist the simplified function
  • 1
    The quality degradation is so real lol
  • 2
    yep,I was rigth.
    if you fix it by your own, you will learn a lot
    #ImAJerk
  • 1
    also, if you want to understant javascript, just put console.log everywhere
  • 0
    Tip: "return output;" When is this called and where is the return value returned?
  • 0
    How's it going, mate? Figured it out yet or do you need more tips?
  • 0
    @TerriToniAX output is called when the request is completed and returned to the innerhtml of "out" element. I even tried writing it differently I still can't find what's wrong. Lol and yes a tip will help
  • 0
    @BurningSatan Nope. It's not returned there. It's returned upon the event onreadystatechanged being fired.
    document.getElementById('out').InnerHTML is set to the return value of *ajReq*. And what does ajReq return? Precisely. Nothing.
  • 2
    ho, you spoiled it 😞
  • 0
    @simus Sorry, he's been stuck with it for days so I just felt I had to give him a firmer push in the right direction :)
  • 1
    me to, but it's the best way to learn.
    @BurningSatan javascript is a bitch, never trust it, console.log is your best friend to understand how this mf work
  • 0
    @theScientist looks like devrant reduces quality
  • 1
    @BurningSatan Have you figured it out?
  • 0
    @TerriToniAX @simus
    I rewrote the code and now it works. Thanks. Btw I still can't figure out what was wrong with the previous code.
  • 1
    @BurningSatan In your new code, you don't seem to set the contents of HTML element "out" anywhere. I take it that "it works" in this case means that you get expected output using the console.log method. It works because you output the responsetext inside the anonymous callback function instead of returning it to the event that triggered the callback. (The caller event does nothing with the return value). Don't worry, asynchronous code can be cumberlicated at first, but once you master it it's quite straightforward actually. As @simus said, think asynchronous. I'd like to add to that, think of each event as a thread, think reentrant.
Add Comment