0

The jquery .ajax() request returns nothing on the first attempt. Only if I fire the same function again does it return the correct data.

Here is the code I use to solve the issue (still the cause is unknown):

   $(document).ready(function(){
    $(window).load(function(){
      $('input').change(function() {
        $('#b').removeClass("valid");
           if ($('#b').val()) {
             bajax(false);
           }
        }
        function bajax(e) {
           $.ajax({
              type: "GET",
              dataType: 'text',
              data: 'i='+$('#b').val(),
              cache: false,
              url: 'b.php',
              success: function(data){
                 $('#b').val(data);
                 $('#b').addClass("valid");
              },
              error: function() {
                 if (!e) {
                    bajax(true);
                 } else {
                    $('#b').val("An error occured.");
                    $('#b').removeClass("valid");
                 }
              }
        });
      });
    });
   });

#b is a simple <input>, and b.php outputs a short string. I am testing it right now with echo "123"; and as I said it works, except that I have to fire this request twice to get the text to show up.

EDIT: console.log output:

[16:47:54.893] "Running..."
[16:47:55.096] GET b.php?i=19&_=1388245674895 [0ms]
[16:47:55.583] "Call to error function"
[16:47:59.595] GET b.php?i=19&_=1388245679534 [0ms]
[16:47:59.533] "Running..."
[16:48:00.025] "Came into the success function with" "123"
Sparky
  • 98,165
  • 25
  • 199
  • 285
Kolja
  • 860
  • 4
  • 10
  • 30
  • @Juhana - `async : false` – adeneo Dec 28 '13 at 15:18
  • Thanks, I checked the thread you refer to but that's not the issue I have. – Kolja Dec 28 '13 at 15:19
  • 6
    OMG, there are still people setting `async: false` in 2013 (soon to be 2014) and thinking that they are using AJAX. Incredible. – Darin Dimitrov Dec 28 '13 at 15:19
  • @Darin Dimitrov I know that this is not AJAX, but that's not the point. If you have a better idea on how to get the output from b.php without using .ajax(), please let me know. I am happy to try another solution. – Kolja Dec 28 '13 at 15:23
  • 2
    Sure, for starters, get rid of the `async: false`. Also get rid of those global `format` variables and simply do whatever you wanted to do with the result of your AJAX call inside the `success` callback. Also stop thinking in procedural sequential code. Start thinking in terms of asynchronous code and callbacks. That's what AJAX is all about. – Darin Dimitrov Dec 28 '13 at 15:28
  • is this correct `data: 'i='+x,` you should try `{i : x}` and `dataType : "text"` if string is expected form the response. – Jai Dec 28 '13 at 15:29
  • Get rid of the `async: false` and do the work inside the success function. Like in my answer. – aychedee Dec 28 '13 at 15:29
  • did you log xhr in you console? are there two xhr request to b.php? – Daiwei Dec 28 '13 at 15:36
  • Don't edit your answer to be a completely different question... It just means that none of the answers or comments make any sense now. – aychedee Dec 28 '13 at 15:36
  • Also, you are calling addClass on the return value of val(). I don't think the return value of `val()` is a jquery object... – aychedee Dec 28 '13 at 15:37
  • @aychedee `.val()` is (now _was_) used as a _setter_ here, it sets the value and returns the jQuery collection! – Ram Dec 28 '13 at 15:40
  • Oh that's true. My bad. I was just misreading it. Ah Jquery methods. That either return something or set something and return something completely different. What amazing design clarity. – aychedee Dec 28 '13 at 15:45
  • What is your firing or triggering event? – Krish R Dec 28 '13 at 15:49
  • Are you sure the DOM is ready when you make the first call? – veritasetratio Dec 28 '13 at 15:50
  • yes, see above, I added (document).ready, etc. to my question – Kolja Dec 28 '13 at 15:54
  • 1
    FYI - do not ever edit your question's title with "solved" or similar on SO. Instead, post the solution below as an answer. Of course it's a moot point in this case, since the whole thing was closed as a duplicate. – Sparky Dec 28 '13 at 17:24
  • @Sparky Will do. Honestly I was a bit pissed off because my question is not at all duplicate. I checked the thread the different people marking this question referred to, but it was not my issue. This just shows the people haven't read my question or got pissed off because they didn't know the solution themselves, or whatever. Anyways, that doesn't matter now as my problem is (more or less) solved. See you on the next question. :D – Kolja Dec 28 '13 at 18:53
  • Or it show that maybe the question wasn't written clearly enough. If it matters, you could edit your question to clarify why it's not a duplicate and then vote and/or flag it for re-opening. There is no need for you to read so much emotion into this. If you believe you have a legitimate gripe that the folks who voted to close _"got pissed off because they didn't know the solution"_, feel free to take this over to the [Meta site](http://meta.stackoverflow.com/) for closer examination. – Sparky Dec 28 '13 at 19:00

0 Answers0