jQuery Question

TechBoyJK

Lifer
Oct 17, 2002
16,699
60
91
Hi Guys,

I'm trying to wrap my mind around javascript and using jQuery for ajax. I'm building an example form (just a button for now) using a few remote functions I wrote that return json results.

#apiSubmit is the submit button id
#applyform is the form div id
#popUpDiv is the "waiting to process" div

When someone clicks the 'apiSubmit' button, jquery will send a getJSON request to a function that checks if thier userID is on a list. It will return {error:0} if successful and {error:"entry exists"} if there's a match. If there's an error, it does a basic javascript 'alert' displaying the json results error value ie "entry exists". The nature of the script I'm building this example from is doing the request to make sure a userID isn't in a table, that's why it's an error if it's there. If it's successful (no id match), then it fires off another getJson method adding that userID to the list. Same deal on error results. {error:0} if successful and {error:"entry exists"} I know the previous method already checked if the entry exists but I think it's reasonable to throw the error again if the actual insert throws an error. In more complex scripts I'd be more likely to do this.

Everything works up to the if/then statement. If I comment it out it fires off the first JSON method. Can someone look at my if/then statement and tell me how to get the syntax right? Logically I think it's right. Hopefully someone can deduct what I'm trying to do and note the corrections? If I can see how this basic script is put together, I think I'll be ok. I have scripting experience in other languages but I haven't used jquery much.

*This is contained within a CFML markup document, so hashtags need to be escaped using double ##, stuff within hash tags are cfml variables.


Code:
$("##apiSubmit").click(function(){

   $("##applyForm").toggle();
   $("##popUpDiv").fadeIn(1000);

   $.getJSON("http://myco.net/com.cfc?method=checkUser&userID="+ #session.myID#, function(checkUserResults){} );

   if (checkUserResults.error != '0') {
      alert(checkUserResults.error);
   }

   else {

      $.getJSON("http://myco.net/com.cfc?method=createUser&userID="+ #userID#,function(createUserResults){});

      if (createUserResults.error != '0') {
         alert(createUserResults.error);
         $("##applyForm").toggle();
      }
      
      else {
         $("##popUpDiv").fadeOut(1000);
      }

   }
});
 
Last edited:

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Put your if/else inside the callback,


$.getJSON("http://myco.net/com.cfc?method=checkUser&userID="+ #session.myID#, function(checkUserResults){} );

Whatever function you specify to $.getJSON, that is what gets called after the request has been made. The if/else gets parsed before that. Throw everything into that callback, so basically nest it.

This is because the request is asynchronous, aka not synchronous, so the if/else doesn't wait for it to happen. The other solution is to make it synchronous, but that kinda defeats the purpose of "ajax".
 

jsedlak

Senior member
Mar 2, 2008
278
0
71
What Woosta said.

AJAX is just "asynchronouse javascript and xml" by the way. The getJSON method (terrible method name btw) will return immediately with the request object, not the response from the server (which is handled via a callback).
 

DannyBoy

Diamond Member
Nov 27, 2002
8,820
2
81
www.danj.me
As has been noted you need to move your conditions into the callback:

Code:
  $.getJSON("http://myco.net/com.cfc?method=checkUser&userID="+ #session.myID#, function( data, textStatus ){
      // This is your callback scope

      if( checkUserThingy.error ) { 
            // Do something
      }

   });

Alternatively you can define your callback function somewhere else and reference it in your call to $.getJSON

Code:
// Callback function
var myCallback = function( data, textStatus, xhrObj ) {
   // Condition
   if ( checkUserThingy.error ) {
      // Do something
   }
}

// Call $.getJSON
$.getJSON("http://myco.net/com.cfc?method=checkUser&userID="+ #session.myID#, myCallback );
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |