当我做一个AJAX调用(见下文code),什么是数据。我如何设置和获取数据的
// $。员额()
$(#邮报)。点击(函数(){
$(#结果)HTML(ajax_load);
$。员额(
使用loadURL,
{语:PHP,版本:5},
功能(数据){
$(#结果)HTML(数据);
},
JSON
);
});
解决方案
该数据是您输入的序列化值。例如:
<形式GT;
<输入类型=文本名称='myText1'值='你好'/>
<输入类型=文本名称='myText2'值=世界/>
< /形式GT;
您现在可以运行这样的:
VAR的myData = $('表')序列化()。
警报(myData的);
和你的消息框会说:
myText1 =你好&放大器; myText2 =世界
的myData 的是要传递到.post的$函数的数据值。
既然你是新来的jQuery,我想也许建议您尝试使用 $就函数。有它有更多的选择,但我一直认为这是更简单,更容易理解比$。员额。下面是我使用它:
$。阿贾克斯({
类型:POST,//定义AJAX调用的类型(POST,GET等)
网址:我-AJAX-的script.php,//要调用的脚本的名称
数据:myData的,//你的数据要发送到脚本
成功:函数(MSG){
$(#结果)HTML(MSG); //你产生行动
}
});
顺便说一句,不要忘了,为了使用jQuery的序列化功能,所有的投入需要有名称的属性设置,否则序列化功能会忽略它们。
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"
);
});
解决方案
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.