Results 1 to 6 of 6
  1.    #1  
    Is there a way that I can send XML within the Prototype framework or through Mojo and then wait for a response?
  2. #2  

    the way that you wrote that question makes it clear that you don't know how asyncronous operations happen in Javascript.

    When you do an ajax send, the send defines two cases to call OTHER functions...

    So, you have some function "SendMyRequest" and it has an onsuccess and an onfailure

    onsucess it calls the onsuccess function, and on failure it calls the on failure function.

    So, if your program reaches the point where it needs to send the ajax request, there shouldn't BE any code after that in the call. It just isn't RUNNING anything when onsuccess happens, it PICKS UP with the beginning of the onsuccess function and runs IT.

    if the ajax call fails, you need to handle that (in an airplane, in a basement, no network connection etc.)

    if you need to make a series of ajax calls one after another __DO NOT__ try to code them in a loop....

    do something like this:

    count = -1
    addresses = [url1, url2, url3, url4]

    function getStuff(count) {
    this.count = count
    var myUrl = addresses[this.count];
    do ajax call
    onsuccess do gotIt(result) ;
    onFailure do didNotGotIt() ;

    function gotIt(result) {
    do something with result
    if (count < addresses.length) {
    count += 1
    } else {

    see the idea? When the first ajax call completes, it calls gotIt. when gotit completes, it calls getstuff AGAIN with the next url which calls got it which calls getstuff until all the url's are gotten

    ((( Don't forget to write the error handler on the failure option)))

    then, when the LAST url is gotten, you call the "NEXT STEP AFTER I GOT STUFF function and the program goes on.

    There's never a piece of CODE that says "wait for a reply" it's just INHERENT in the nature of the onsuccess and onfailure calls. Cool huh?

  3.    #3  
    I understand asynchronous just fine actually Rick.

    Really, in retrospect it was a poorly worded question. How do I make the initial send call to send my XML? Something like below. Obviously, this is pseudo code, cause if this was right I would not be asking.

    I get how to wait for the call back but I don't get how to send my information initially.


    var myString = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><stuff>Hello</stuff>";
  4. #4  
    Ahhh. Well, sorry, the way it was worded, it sounded like you were asking how to WAIT.... :-)

    which is the most common thing we answer around here. Async is not an obvious thing to a lot of programmers.

    basically everything you want is documented at Prototype API documentation | Ajax section

    the short answer is
    new Ajax.Request('yourURL',   onSuccess: function(response) { something }, 
         onFailure { something }  );
  5.    #5  

    I have it working now. I will post my code shortly just so it's clear how I eventually solved this problem for archival purposes.

    Last edited by RLovelett; 09/20/2009 at 05:47 PM. Reason: Solved problem.
  6.    #6  
    As promised here is the code required to send information stored in a string (such as XML). You could just as well use parameters in stead of postBody if you are wanting to send an object. There are a few different ways of doing this, but this is what I used to send my XML. See the link above to see all of the options that are allowed in the request.

    var myAjax = new Ajax.Request(Caller.DEFAULT_API_ROOT, {
    	encoding: 'UTF-8',
    	method: 'post',
    	evalJSON: true,
    	secret: secret,
    	onSuccess: response,
    	onFailure: function(response) {Mojo.Log.error("I failed! \n%j", response.transport);},
    	postBody: myString

Posting Permissions