Umbraco version 6.1.6. I am new to WebApi and Examine and I've spent all day trying to return JSON using WebApi to a clientside jquery ajax call. Despite loads of googling and searhing this forum I can't quite find what I need.
I want to find all instances of a pages created of a particular type "PageType". I then want to return this 'List' of pages back as JSON to the client.
I've cobbled together the following using various snippets found on Google but I am clearly missing something fundamental.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Umbraco.Core; using Umbraco.Web.WebApi; using System.Collections; using Umbraco.Web; using Examine; using Newtonsoft.Json;
public class ThisIsMyApiController : UmbracoApiController { public IEnumerable GetPages() { UmbracoHelper help = new UmbracoHelper(UmbracoContext); var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content"); var filter = criteria.NodeTypeAlias("PageType"); var result = Examine.ExamineManager.Instance.Search(filter.Compile()).ToList(); return result;
} }
HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: application/xml; charset=utf-8
Date: Sun, 01 Dec 2013 21:15:34 GMT
Content-Length: 3374
<Error><Message>An error has occurred.</Message><ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage>
If I change the return line to serialize the object myself using Newtonsoft.json then I get a bit further:
return JsonConvert.SerializeObject(result);
then it does complete. But it doesn't return JSON, it returns an XML document with a separate node for every single character in the document as follows:
public List<SearchResult> GetPages()
{
UmbracoHelper help = new UmbracoHelper(UmbracoContext);
var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");
var filter = criteria.NodeTypeAlias("PageType");
var result = Examine.ExamineManager.Instance.Search(filter.Compile()).ToList();
return result;
}
}
When you hit the web api directly with browser you get xml but when you use jquery and the like you do get back json. I think your issue at the moment is that the search is not returning anything. In your code when you return result before the return can you do criteria.ToString() and write that out or look at it using debugger and get a copy of the generated examine query. Then try this using examine inspector or luke and see if you get any results.
I quite like this option too. Doesn't completely remove the XML response should you want it (and request text/xml), but makes the browser default JSON.
WebApi return JSON from Examine search
Umbraco version 6.1.6. I am new to WebApi and Examine and I've spent all day trying to return JSON using WebApi to a clientside jquery ajax call. Despite loads of googling and searhing this forum I can't quite find what I need.
I want to find all instances of a pages created of a particular type "PageType". I then want to return this 'List' of pages back as JSON to the client.
I've cobbled together the following using various snippets found on Google but I am clearly missing something fundamental.
If I then go to http://localhost/umbraco/api/ThisIsMyApi/GetPages
Fiddler returns this:
If I change the return line to serialize the object myself using Newtonsoft.json then I get a bit further:
then it does complete. But it doesn't return JSON, it returns an XML document with a separate node for every single character in the document as follows:
As well as getting the above working, I'm also keen to know is it is the best way of achieving what I need to do?
Thanks in advance
Travis,
When you hit the web api directly with browser you get xml but when you use jquery and the like you do get back json. I think your issue at the moment is that the search is not returning anything. In your code when you return result before the return can you do criteria.ToString() and write that out or look at it using debugger and get a copy of the generated examine query. Then try this using examine inspector or luke and see if you get any results.
Regards
Ismail
Travis,
Just an update on this. If you implement IApplicationEventHandler and in OnApplicationStarting do
This will ensure json is returned by default. Just had to do this and it works a treat.
I quite like this option too. Doesn't completely remove the XML response should you want it (and request text/xml), but makes the browser default JSON.
Andy
Andy,
Yup saw that but xml is dead json is the future lol joking of course!!!
is working on a reply...