1

Working with this documentation and jQuery ajax to use the search features of the REST api.

I'm making progress and it is starting to make some sense. I'm trying to figure out how to work with the results. The tutorials online talk about how to create the request, but not how to deal with what comes back.

I'm using jQuery ajax and promises to get the response in json format like this:

function doSearch(terms){

  // make the queryObj

  return $.ajax({
    url:'site/_api/search/postquery',
    method:'POST',
    data:JSON.stringify(queryObj),
    headers:DEFAULT.headers 
  })
}

doSearch("someterm")
  .done(function(data,status){
    var results = data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows
  });

Is that really how I should be getting at the results?

It seems amazing to me that the actual results (the whole point of the query) are buried so far down in the objects. Also this is a POST query, if I change to a GET query it starts with data.d.query... then the rest is the same.

Is there some property or part of the result that I haven't noticed yet that let's me access the results more directly or in a more idiomatic way?

Does anyone know a good tutorial on how to work with the results?

EDIT Okay a second question. Let's say I asked to retrieve the ParentLink property. How do I get that value out of the table? Do I have to iterate through each cell in the table and test if it has the Key of "ParentLink" or is there some kind of way of saying something like this:

myResults.getProperty("ParentLink")

If not is there a way to change the results into json?

0

To return JSON, you need to add

contentType: "application/json;odata=verbose", headers: { "accept": "application/json;odata=verbose", }

to your ajax object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.