I am working on a Web API application which has a drop down list that should be populated with API response which is in xml using knockout JS. I was trying using the following
$.ajax({
dataType: "json",
url: '/api/UserProfiles',
data: JSON.stringify(self.brokerNames),
async: false,
success: function (data) {
self.brokerNames((ko.utils.arrayMap(data.value, function (broker) {
var obsBrokers = {
UserId: broker.UserId,
UserName: broker.UserName
}
return obsBrokers;
})));
}
});
I tried binding above response to a drop down as follows
<td>
<select id="cbxBCP" data-bind="options: brokerNames,
optionsText: 'UserName', optionsValue: 'UserName',
value: selectedBidBroker, optionsCaption: 'Bid Broker'">
</select>
</td>
But, the above returned an empty drop down. I'm not sure what the issue is but I saw the XML response when debugging with Postman.
The Web API is this
[HttpGet]
[Authorize(Roles="Admin")]
public IEnumerable<UserProfile> Get()
{
return db.UserProfiles.OrderBy(c => c.UserName);
}
and seems to work fine.
May I know how I can fix this?
datais your returned XML, sodata.valuewould be undefined. I don't see any attempt to parse the XML at all; this code appears to be written for a JSON response.datainstead ofdata.value? You don't make clear what verb is usign your action, and if it's expecting parameters: why do you add data to your $.ajax()? read the dcs and see samples for $.gethttpgetand I still get xml response and I'm not sure how to parse it to json and pu it in dropdown.. edited my question