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 3 of 3
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    return 2 values in ajax

    Code:
    $.ajax({
    	url: urltoajax,
    	cache: false,
    	success: function(html){
    	$("#sptable").html(html);
    	}
    	});
    above is my current code. In addition to that url I also want to return to another javascript variable - html code retieved from calling a different asp page in ajax.

    How can i do this?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Is it JQuery? Prototype? MooTools? Other framework?

    As it is not native javascript, it is hard to know what is there...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    This is jQuery.

    And the answer is you should use JSON: JavaScript Object Notation.

    the $.ajax() function in jQuery can take an object literal loaded with all kinds of settings. You have 3 you're using already: url, cache, and success. One of them is called 'dataType' and it allows you to specify the kind of data you're expecting to receive from the server.

    Code:
    $.ajax({
      //url, cache, success settings, etc.
      dataType: "json"
    });
    Then you just have to have your server return a string that is JSON. An easy way to do this in PHP is with the json_encode() function. You can create an array of results and then pass them to json_encode() to turn them into JavaScript object literals. If you were getting results from a database you may end up returning a string that looks something like this:

    Code:
    {"name":"Dean", "age":23, "gender":"M", "job":"stamp licker", "department":"collections", "payRate":5.15};
    From there in JavaScript you can easily display or manipulate this information.

    Code:
    alert(data["name"]); //Dean
    alert(data.age); // 23
    Last edited by Dean440; 07-02-2010 at 04:26 PM.


  •  

    Posting Permissions

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