Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    127.0.0.1
    Posts
    67
    Thanks
    19
    Thanked 0 Times in 0 Posts

    ajax script not working :( plz help

    My code:

    JAVASCRIPT PART:
    Code:
    function test() {
    	$.ajax({
    		url: '../ajax/test.php',
    	}).done(function(data) { 
    		return data;
    	});
    }
    alert(test());
    PHP PART:
    Code:
    <?php
    	echo "Hello, world!";
    ?>

    What I'm doing wrong ? :/



    PS: Im loaing jQuery mini 1.8 from jquery website.
    My website is here: http://www.moowdesign.eu/. It is not complete yet, and I want to add language translations for some languages(including english).

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Do you mean to go back one directory/folder and then access a sub-folder called "ajax"?

    If the URL is the only argument, remove the comma after test.php',
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You tried to return something from your "outer" function test(), but instead you only return something from the "inner" anonymous closure function.

    General rule: Every action that depends on the outcome of an asynchronous method must be run inside the callback of that method or must be called from that callback. Returning anything doesn't make any sense because at the time the callback is run, the outer function has already finished.

  • #4
    New Coder
    Join Date
    Sep 2011
    Location
    127.0.0.1
    Posts
    67
    Thanks
    19
    Thanked 0 Times in 0 Posts
    Soo.....How can I edit it, so it will work?

    PS: removing that comma after test.php' did not affect anything
    Last edited by sonic656; 11-14-2012 at 07:48 PM.
    My website is here: http://www.moowdesign.eu/. It is not complete yet, and I want to add language translations for some languages(including english).

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Whatever you want to do with "data", just do it inside the .done() method

  • Users who have thanked devnull69 for this post:

    sonic656 (11-15-2012)

  • #6
    New Coder
    Join Date
    Sep 2011
    Location
    127.0.0.1
    Posts
    67
    Thanks
    19
    Thanked 0 Times in 0 Posts
    and what with the return code? how can I return the data?
    My website is here: http://www.moowdesign.eu/. It is not complete yet, and I want to add language translations for some languages(including english).

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You didn't get me ... technically you can return something from each and every function, but semantically it doesn't make any sense to return something from the callback of an asynchronous method because it will end up "in thin air". The calling method has long finished before the callback is executed so you cannot return anything from it.

    So:
    General rule: Every action that depends on the outcome of an asynchronous method must be run inside the callback of that method or must be called from that callback. Returning anything doesn't make any sense because at the time the callback is run, the outer function has already finished.

  • #8
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    In other words, I don't think you can "return" anything for the alert.. now you should be able to put the alert inside the callback function. Or, alternatively, create a div, give it an id, and use .html() to place the data in it.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •