简体   繁体   English

如何使用Ajax获取有关ID的所有信息

[英]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.

相关问题 如何获取有关.innerText的所有样式信息 - How can I get all the style information about .innerText 如何使用JavaScript在样式表中获取有关图像的信息 - How can I get information about images in a stylesheet using JavaScript 如何使用 foreach 获取 object 数组中的所有 Id 并合并所有数据对应的 ID? - How can I get all Id in array of object by using foreach and merge the all data corresponding ID? Javascript使用有关父元素的信息获取子元素的ID - Javascript get ID of a child element using information about the parent 如何从 GitHub API 获取所有信息 - How can I get all the information from the GitHub API 我可以使用javascript和google-analytics获得有关用户地理位置的信息吗? - Can I get information about user's geolocation using javascript and google-analytics? 如何通过live()获得有关绑定到元素的事件的信息? - How can I get information about events that are bound to an element via live()? 我如何获得有关未通过覆盖添加的单击标记的信息? - How can I get information about a clicked marker which was not added via overlay? 如何保存使用Ajax选择的信息? - How can save the information selected using ajax? 如何将文本框的 id 与 ckeditor 一起使用(与 ajax 一起使用) - how can I use the id of textbox with ckeditor (using with ajax)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM