jquery $ .post函数的结果数据

1 ajax jquery post json

当我进行ajax调用时(参见下面的代码),什么是"数据".如何设置和获取数据

//  $.post()  
 $("#post").click(function(){  
     $("#result").html(ajax_load);  
     $.post(  
         loadUrl,  
         {language: "php", version: 5},  
         function(data){  
             $("#result").html(data);  
         },  
         "json"  
     );  
 });
Run Code Online (Sandbox Code Playgroud)

Jak*_*son 5

数据是输入的序列化值.例:

<form>
    <input type='text' name='myText1' value='hello'/>
    <input type='text' name='myText2' value='world'/>
</form>
Run Code Online (Sandbox Code Playgroud)

你现在可以运行这个:

var myData = $('form').serialize();
alert(myData);
Run Code Online (Sandbox Code Playgroud)

你的消息框会说:

myText1=hello&myText2=world
Run Code Online (Sandbox Code Playgroud)

myData是您要传递到$ .post函数的数据值.

由于您不熟悉jQuery,我建议您尝试使用$ .ajax函数.它有很多选择,但我一直认为它比$ .post更直接,更容易理解.这是我如何使用它:

$.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
    }
});
Run Code Online (Sandbox Code Playgroud)

顺便说一句,别忘了,为了使用jQuery serialize函数,所有输入都需要设置name属性,否则serialize函数会忽略它们.