jquery $.post function's result data

前端 未结 4 1133
死守一世寂寞
死守一世寂寞 2021-01-16 09:18

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

//  $.post()  
 $(\"#post\").click(function(){  
     $(\"#result\").h         


        
4条回答
  •  攒了一身酷
    2021-01-16 09:50

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

    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.

提交回复
热议问题