How to access search service api to get results in JSON format? RRS feed

  • Question

  • How to access search service api to get results in JSON format in web application outside SharePoint farm. It should be an app or SharePoint hosted.

    I have tried the same with the help of "".

    function onQuerySuccess(data) {

        var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;




        $.each(results, function () {


            $.each(this.Cells.results, function () {

                $("#resultsDiv").append('<td>' + this.Value + '</td>');







    But we are getting data.d as undefined.

    I have checked and verify that I am getting success and data in XML format not in JSON. As I expected the JSON. Although I have specified the "JSON" in headers " headers: { "Accept""application/json; odata=verbose" } ". 


    Monday, February 23, 2015 10:26 AM


  • Hi,

    Per my understanding, you might want to access search service REST API and get results in JSON format using jQuery.

    The code you use is working in a SharePoint Hosted App project, to compose a working code snippet easier, you can take the demo below for a try:

    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
    function ready()
    	var spAppWebUrl = _spPageContextInfo.webAbsoluteUrl;
    	var queryUrl = spAppWebUrl + "/_api/search/query?querytext='book'";
    		url: queryUrl, 
    		method: "GET", 
    		headers: { "Accept": "application/json; odata=verbose" }, 
    		success: function (data) {        
    			var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
    			$.each(results, function () {
    				$.each(this.Cells.results, function () {
    	    error: function (data) {

    Add the code into a page of your site, it will retrieve JSON format data and print the search result in the console of browser.

    More information about how to work with SharePoint Hosted App:

    If you want to call the search service REST API in a remote environment, authentication is required to be taken into consideration, the link below with a working demo would be helpful:

    Best regards

    Patrick Liang
    TechNet Community Support

    Tuesday, February 24, 2015 7:46 AM