如何使用$阿贾克斯()jQuery的发送多个数据 [英] how to send multiple data with $.ajax() jquery
问题描述
我试图使用j个查询$就法我的PHP脚本发送多个数据,但我可以通过唯一的单数据,当我连接多个数据得到了一个未定义的索引错误在我的PHP脚本针锋相对意味着Ajax请求作出,但数据不会发送我需要知道我应该如何格式的多个数据连续地发送给在这里的名字淡水河谷对处理脚本是我自己写的
<脚本>
$(文件)。就绪(函数(){
$('#加)。点击(函数(){
变种名称= $('#加')ATTR(data_id)。
VAR ID = $('#加')ATTR(UID)。
VAR数据='ID ='+ ID和放大器; NAME ='+名; //这其中,我添加使用'和多个数据;
$阿贾克斯({
键入:GET,
缓存:假的,
网址:的welcome.php
数据:数据,//使用AJAX发送多个数据
成功:函数(HTML){
$('#加)VAL(发送的数据发送)。
$('#味精)HTML(HTML);
}
});
返回false;
});
});
< / SCRIPT>
<跨度>
<输入类型=按钮级=gray_button值=发送数据ID =添加data_id =1234的uid =4567/>
< / SPAN>
<跨度ID =味精>< / SPAN>
您可以创建键/值对的对象和jQuery将完成剩下的你:
$。阿贾克斯({
...
数据:{FOO:'吧,吧:'富'},
...
});
这样的数据将是正确的连接codeD自动。如果你想炮制你自己的字符串,那么要确保使用连接codeURIComponent()
:<一href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/en$c$cURIComponent">https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/en$c$cURIComponent
您当前的code不工作,因为字符串是不正确炮制的:
'ID ='+ ID和放大器; NAME ='+名
应该是:
'ID ='+ EN codeURIComponent(ID)+'&放大器;名称='+ EN codeURIComponent(名)
i am trying to send multiple data using j query $.ajax method to my php script but i can pass only single data when i concatenate multiple data i get undefined index error in my php script tat means the ajax request is made but data is not sent i need to know how should i format multiple data to successively send it to processing script in name vale pair here is what i have written
<script>
$(document).ready(function() {
$('#add').click(function () {
var name = $('#add').attr("data_id");
var id = $('#add').attr("uid");
var data = 'id='+ id & 'name='+ name; // this where i add multiple data using ' & '
$.ajax({
type:"GET",
cache:false,
url:"welcome.php",
data:data, // multiple data sent using ajax
success: function (html) {
$('#add').val('data sent sent');
$('#msg').html(html);
}
});
return false;
});
});
</script>
<span>
<input type="button" class="gray_button" value="send data" id="add" data_id="1234" uid="4567" />
</span>
<span id="msg"></span>
You can create an object of key/value pairs and jQuery will do the rest for you:
$.ajax({
...
data : { foo : 'bar', bar : 'foo' },
...
});
This way the data will be properly encoded automatically. If you do want to concoct you own string then make sure to use encodeURIComponent()
: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
Your current code is not working because the string is not concocted properly:
'id='+ id & 'name='+ name
should be:
'id='+ encodeURIComponent(id) + '&name='+ encodeURIComponent(name)
这篇关于如何使用$阿贾克斯()jQuery的发送多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!