Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning JSON with web api controller MVC
    text
    copied!<p>I am trying to convert a regular old controller I was using to an API controller and am having a little bit of difficulty. What these series of functions do is, in the jQuery, it iterates over a file containing all the usernames of employees and for each username it makes a call to the PopulateEmployee method in my webapi controller which should return JSON and then populate a results div. </p> <p>When manually navigating to ..domain../staffinformation/populateemployee/employeeusername</p> <p>I get the error </p> <pre><code>This XML file does not appear to have any style information associated with it. The document tree is shown below. &lt;Error&gt; &lt;Message&gt; The requested resource does not support http method 'GET'. &lt;/Message&gt; &lt;/Error&gt; </code></pre> <p>Please note that the div it will be populating is a partial view in an Umbraco CMS page and I don't think that is the problem but if you guys think differently please tell me.</p> <p>There has to be something I am missing either with webAPI routing or something else.</p> <p>Thanks for your help.</p> <p>Here's the codez.</p> <p>Please notice that this method has the HttpPost tag</p> <pre><code>public class StaffInformationController : ApiController { [System.Web.Http.ActionName("PopulateEmployee")] [System.Web.Http.HttpPost] public StaffListing PopulateEmployee(string id) { //do error checking on input StaffListing staffListing = new StaffListing(id); //populate other fields return staffListing; } } </code></pre> <p>The routing set up for the api controller</p> <pre><code>public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); } } </code></pre> <p>The jQuery call specifying use of 'POST', please forgive the trickiness of the recursive call in this function.</p> <pre><code>function getEmployeeObjectByIndex() { $.ajax({ url: $('#root').val() + '/api/StaffInformation/PopulateEmployee', type: 'POST', async: true, contentType: 'application/json, charset=utf-8', data: JSON.stringify({ 'username': lines[i] }), success: function (staffObject) { if (!(staffObject.Name == undefined)) { buildHtmlStrings(staffObject); } i++; getEmployeeObjectByIndex(); //recursive call } }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload