[英]How can I get all the information about an ID using Ajax
I have a question using Ajax call, I have this code below : 我有一个使用Ajax调用的问题,我有以下代码:
$.ajax({
async: true,
url: 'test/',
type: 'POST',
datatype: 'text json',
data: {
id: id,
},
success: function(data) {
// Get the data of the id (1)
},
error: function(xhr, errmsg, err) {
}
});
And I would like to do a query which allows to get all the information with the ID in data there is the id and I think I have to write the information to get the data of the id at (1). 我想做一个查询,它允许在数据中获取ID的所有信息,并且我认为我必须编写信息以获取id的数据(1)。 But I don't know how doing this, could you help me please?
但我不知道怎么做,你能帮帮我吗?
Thank you. 谢谢。
You did't attached example of returning data 你没有附上返回数据的例子
if it's array of objects and you need the first one you can use var firstObj = data[0];
如果它是对象数组而你需要第一个对象你可以使用
var firstObj = data[0];
If your object has Id field you should use filter to find it 如果您的对象具有Id字段,则应使用过滤器来查找它
firstObj = data.filter(function (item)
{
return item.ID == 1 //if true object will be pushed to returned array
})[0]; //get only first (and I assume unique) result
if you want all information related to a particular id then you can go with form
$("form").serialize(); for example
<form id="myForm">
//mention components text,radio,etc between this form tag
</form>
and finally when control goes to ajax call,then your code should be
$.ajax({
async: true,
url: 'test/',
type: 'POST',
datatype: 'text json',
data:$('#myForm').serialize(),
success: function(data) {
},
error: function(xhr, errmsg, err) {
}
});
through this approach your all data will be in data attribute.
let me know if it works for you.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.