1

When I make an ajax call (see code below), what is "data". How do I set and get data

//  $.post()  
 $("#post").click(function(){  
     $("#result").html(ajax_load);  
     $.post(  
         loadUrl,  
         {language: "php", version: 5},  
         function(data){  
             $("#result").html(data);  
         },  
         "json"  
     );  
 });
Matt
  • 41,216
  • 30
  • 109
  • 147

4 Answers4

5

The data is a serialized values of your inputs. Example:

<form>
    <input type='text' name='myText1' value='hello'/>
    <input type='text' name='myText2' value='world'/>
</form>

You could now run this:

var myData = $('form').serialize();
alert(myData);

And your messagebox would say:

myText1=hello&myText2=world

myData is the data value that you want to pass into the $.post function.

Since you are new to jQuery, I'd perhaps recommend you try using the $.ajax function instead. There are a lot more options for it, but I always thought it was more straightforward and easier to understand than $.post. Here is how I'd use it:

$.ajax({
    type: "POST",    //define the type of ajax call (POST, GET, etc)
    url: "my-ajax-script.php",   //The name of the script you are calling
    data: myData,    //Your data you are sending to the script
    success: function(msg){
        $("#result").html(msg);   //Your resulting action
    }
});

Btw, don't forget, in order to use the jQuery serialize function, all the inputs need to have the name attribute set, or else the serialize function will ignore them.

Christian
  • 1,017
  • 3
  • 14
  • 30
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
1

The documentation for $.post says that data "could be xmlDoc, jsonObj, html, text, etc...". It's whatever the server returns for the loadUrl you specified with the given parameters (in your case, language: "php", version: 5), so you need to examine what the server is returning.

Just alert(data) in your callback and you'll see what was returned.

Update: renamed 'responseText to 'data', since the OP changed the question to do that.

aem
  • 3,896
  • 24
  • 20
  • I think he's asking what the data input is, not what the return/response is. – Jake Wilson Oct 13 '09 at 18:39
  • @Jakobud Sigh, my response got caught in a renaming/clarification by the OP, which made it sound funny. Does you downvote still apply? – aem Oct 13 '09 at 19:03
  • @Jakobud: I don't think the downvote is warranted. As I understand the question, aem is referring to the right thing. – Crescent Fresh Oct 13 '09 at 19:06
  • Ah okay, the only reason I downvoted was to float the correct answer(s) to the top of the stack, since the OP didn't seem to be choosing a correct answer. How do I remove a downvote w/o upvoting? If I can figure that out, I'll remove it. – Jake Wilson Oct 13 '09 at 20:22
1

For example, I use:

$(document).ready(function(){
$("#btSend").click(function() {
    $.post("/Ajax/script.php", {nome: $("#nome").val(), email: $("#email").val()}, function(data) {
        alert(data);
    });
    return false;
});

});

The script.php return what I want to show, but you can change to make another operation with 'data'. The 'btSend' is a image and the 'nome' and 'email' is html textboxes.

This works :)

Cesar
  • 3,519
  • 2
  • 29
  • 43
0
$.post('fileName.php',{

data: $('#id').val(),
},
function(response)
{
  alert(response);
}
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
s-sharma
  • 1,967
  • 1
  • 15
  • 20