Question on $.post() and $.get() callback function

Discussion in 'App Development' started by blueraincap, Dec 28, 2020.

  1. Can you help explain the callbacks on $.get and $.post.

    The some.php here is a script that returns some json. I don't understand why the former gets called and latter doesn't get called, all else equal.

    Code:
    $( "#sendMessageBtn" ).click( function(event) {
      event.preventDefault();
      var message = $("#chatMessage").val();
      $.post("some.php",  {'message': message},  alert("I am back") );
    });
    Code:
    $( "#sendMessageBtn" ).click( function(event) {
      event.preventDefault();
      var message = $("#chatMessage").val();
      $.post("some.php",  {'message': message},  function(){alert("I am back");} );
    });
    Fkc, I just realised I included some invisible comments before <?php
     
    Last edited: Dec 28, 2020
  2. MrMuppet

    MrMuppet


    Sometimes a shower and a new haircut works wonders.
     
    guru likes this.
  3. wtf? you are using javascript or some other shitass sorry excuse for a language., there is your problem. oh php, haha, christ, good luck
     
  4. Snuskpelle

    Snuskpelle

    While I little memory PHP semantics (hate the language), the striking difference between the first and second code snippet is that the latter passes a new function calling alert whereas the first passes the result of a function call to alert. If the post function doesn't check that the passed argument is a function and call it, alert won't be called in that case. The fault of PHP here is likely in not error reporting a function being passed when it's not used in the caller.
     
  5. otctrade

    otctrade

  6. Not to my surprise, every comment is useless as trump
     
    guest_trader_1 likes this.
  7. tsznecki

    tsznecki

    @blueraincap The 2nd you are passing in an anon function but you don't call it. @Snuskpelle is going the right way.

    Try something like this:

    $.post("some.php", {'message': message}, xxx(); );

    function xxx(){alert("I am back");}

    @stochastix Build me an Airbnb clone without Javascript. We won't hold our breath.
     
  8. jules190

    jules190

    They should both work. So long as some.php returns a success code (200), the success handler will be called (the real question should be why does the first one work).

    The only way I was able to get the first to work, and second to fail was to call a non existent php file (status 404).

    Clearly some.php is not returning 200, it's returning an error code, mostly likely not found (404). You can check the status code by opening the console and going to the Network tab, and then clicking your sendMessageBtn.
     
    Last edited: Dec 29, 2020
    blueraincap likes this.
  9. MrMuppet

    MrMuppet

    then why are you asking stuff like this in a retail trading forum?
    go to quant.stackexchange.com
     
  10. jules190

    jules190

    Just to clarify, they both work in a sense that they should both trigger the alert, but the first is incorrect. You should not be passing the alert directly to the handler, you should be passing in a function.
     
    #10     Dec 29, 2020
    blueraincap likes this.