Sharepoint 2010 - Ajax query only returning object but not value
-
31-12-2020 - |
Question
I am using the following code, but all it is returning is "object object" when I need to return whats actually in the value of the column of the list for each list item. Any suggestions?
The list item I am trying to get is a lookup/dropdown value for the item in the list. I can get any other column data except this one.
//start of jQuery document ready section
jQuery(document).ready(function() {
var basePath = "https://sp.test.com/ts/"; //This is your base site url with "/_api/" appended to the end
var listName = "TestList"; //This is where you put the name of your picture library
//start of Ajax query
jQuery.ajax({
//
//This is where you add the fields you would like to return after the "select=". Separate fields by a ","
url: basePath + "_vti_bin/listdata.svc/"+listName+"?$select=Region1",
//
type: "GET", //This is set to GET so it can pull the data properly
headers: { "Accept": "application/json;odata=verbose" }, //This just sets the data to be returned as json data
success: function (data) //data is a system variable which contains the data returned from the server
{
jQuery.each(data.d.results, function (index, value)
//index is the index of items
//value is the item being receieved
//data.d is the serialized data value from the query, and adding .results to the end gets the results of the serialized data
{
alert(value.Region1);
alert(data.d.results);
});
},
error: function (data)
{
//Error gets output here
alert(data.statusText); //data.statusText displays the status of the call
},
//
complete: function()
{
//alert("ah!");
//Insert any code here that you want to run when the ajax call has completed
}
});
//End of Ajax query
});
//End of jQuery document ready section</script>
Solution
I needed to change the basePath portion that read "Region1" to "Region1&$expand=Region1" and then change the alert to read "value.Region1.Id"
Correct Code:
//start of jQuery document ready section
jQuery(document).ready(function() {
var basePath = "https://sp.test.com/ts/"; //This is your base site url with "/_api/" appended to the end
var listName = "TestList"; //This is where you put the name of your picture library
//start of Ajax query
jQuery.ajax({
//
//This is where you add the fields you would like to return after the "select=". Separate fields by a ","
url: basePath + "_vti_bin/listdata.svc/"+listName+"?$select=Name,Time1,Region1&$expand=Region1",
//
type: "GET", //This is set to GET so it can pull the data properly
headers: { "Accept": "application/json;odata=verbose" }, //This just sets the data to be returned as json data
success: function (data) //data is a system variable which contains the data returned from the server
{
jQuery.each(data.d.results, function (index, value)
//index is the index of items
//value is the item being receieved
//data.d is the serialized data value from the query, and adding .results to the end gets the results of the serialized data
{
alert(value.Name.toString());
alert(value.Region1.Id);
});
},
error: function (data)
{
//Error gets output here
alert(data.statusText); //data.statusText displays the status of the call
},
//
complete: function()
{
//alert("ah!");
//Insert any code here that you want to run when the ajax call has completed
}
});
//End of Ajax query
});
//End of jQuery document ready section</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange