jQuery .each() 如何在这个 object 上使用
jQuery .each() how to use on this object
我得到一些 JSON 并尝试对响应执行 .each。我不确定如何在此 object...我正在尝试获取标题和 SysID。
这是我的 JS:
var siteURL = _spPageContextInfo.webAbsoluteUrl;;
$.ajax({
url: siteURL + "/content/_api/web/lists/getbytitle('topsupportarticles')/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (response) {
if (response.d.results.length > 0) {
console.log(response);
//This section can be used to iterate through data and show it on screen .each should be here
console.log(response.d.results[0].Title);
$(".js-top-support").html("<li><a href=\"/pages/snDetails.aspx?articleID=KB0010606&sysID=" + response.d.results[0].SysID +
"\" class=\"js-top-support-articles\">" + response.d.results[0].Title + "</a><div class=\"x-editable-menu\"><span class=\"btn-edit\"><span class=\"icon-wrench\"></span></span></div></li>");
}
},
error: function (response) {
alert("Error: " + response);
}
});
感谢任何帮助...
最好把代码上传到jsfiddle或codepen上。
试试这个:
var results = response.d.results;
jQuery.each(results, function(key,value) {
console.log(key);
console.log(value);
var sysid = value.SysId;
var title = value.Title;
});
你可以选择这两个东西之一。
- 您可以使用
JSON.parse()
方法将字符串解析为有效 json。
- 使用
dataType:"json"
可以自动将 json 字符串解析为有效的 json .
对我来说,第二种方法更好。所以,你可以这样做:
$.ajax({
url: siteURL + "/content/_api/web/lists/getbytitle('topsupportarticles')/items",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
dataType:"json", //<------------------------ ADDED DATATYPE HERE
success: function(response) {
if (response.d.results.length > 0) {
console.log(response);
//This section can be used to iterate through data and show it on screen .each should be here
console.log(response.d.results[0].Title);
$(".js-top-support").html("<li><a href=\"/pages/snDetails.aspx?articleID=KB0010606&sysID=" + response.d.results[0].SysID +
"\" class=\"js-top-support-articles\">" + response.d.results[0].Title + "</a><div class=\"x-editable-menu\"><span class=\"btn-edit\"><span class=\"icon-wrench\"></span></span></div></li>");
}
},
error: function(response) {
alert("Error: " + response);
}
});
但是你应该注意,如果你只有一个对象,那么你可以在这里使用 response.d.results[0]
,否则使用 for
循环或 $.each()
来迭代对象中的对象数组。
附带说明一下,这里有两个分号:
var siteURL = _spPageContextInfo.webAbsoluteUrl;;
我得到一些 JSON 并尝试对响应执行 .each。我不确定如何在此 object...我正在尝试获取标题和 SysID。
这是我的 JS:
var siteURL = _spPageContextInfo.webAbsoluteUrl;;
$.ajax({
url: siteURL + "/content/_api/web/lists/getbytitle('topsupportarticles')/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (response) {
if (response.d.results.length > 0) {
console.log(response);
//This section can be used to iterate through data and show it on screen .each should be here
console.log(response.d.results[0].Title);
$(".js-top-support").html("<li><a href=\"/pages/snDetails.aspx?articleID=KB0010606&sysID=" + response.d.results[0].SysID +
"\" class=\"js-top-support-articles\">" + response.d.results[0].Title + "</a><div class=\"x-editable-menu\"><span class=\"btn-edit\"><span class=\"icon-wrench\"></span></span></div></li>");
}
},
error: function (response) {
alert("Error: " + response);
}
});
感谢任何帮助...
最好把代码上传到jsfiddle或codepen上。
试试这个:
var results = response.d.results;
jQuery.each(results, function(key,value) {
console.log(key);
console.log(value);
var sysid = value.SysId;
var title = value.Title;
});
你可以选择这两个东西之一。
- 您可以使用
JSON.parse()
方法将字符串解析为有效 json。 - 使用
dataType:"json"
可以自动将 json 字符串解析为有效的 json .
对我来说,第二种方法更好。所以,你可以这样做:
$.ajax({
url: siteURL + "/content/_api/web/lists/getbytitle('topsupportarticles')/items",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
dataType:"json", //<------------------------ ADDED DATATYPE HERE
success: function(response) {
if (response.d.results.length > 0) {
console.log(response);
//This section can be used to iterate through data and show it on screen .each should be here
console.log(response.d.results[0].Title);
$(".js-top-support").html("<li><a href=\"/pages/snDetails.aspx?articleID=KB0010606&sysID=" + response.d.results[0].SysID +
"\" class=\"js-top-support-articles\">" + response.d.results[0].Title + "</a><div class=\"x-editable-menu\"><span class=\"btn-edit\"><span class=\"icon-wrench\"></span></span></div></li>");
}
},
error: function(response) {
alert("Error: " + response);
}
});
但是你应该注意,如果你只有一个对象,那么你可以在这里使用 response.d.results[0]
,否则使用 for
循环或 $.each()
来迭代对象中的对象数组。
附带说明一下,这里有两个分号:
var siteURL = _spPageContextInfo.webAbsoluteUrl;;