This is a migrated thread and some comments may be shown as answers.

SearchBox Client Template - DataItem not defined

3 Answers 97 Views
SearchBox
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 18 Feb 2014, 07:45 PM
Hello,

I'm using a client template in a search box. The box is bound to a web service and the template looks like this:
<div class="searchResult">
      <div style="font-weight:bold;">#= Text #</div>
      <div style="font-size:.8em; font-style:italic;">#= DataItem.notes #</div>
</div>

Things are working just fine in Chrome and IE but, in Firefox, I'm getting an error in firebug that the DataItem is not defined:

Sys.InvalidOperationException: Sys.InvalidOperationException: Error rendering template: DataItem is not defined. There's no reason why the web service would return the dataitem for Chrome and IE but not firefox... right?

Any ideas?

Thanks,

Mike

3 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 18 Feb 2014, 08:33 PM
As a follow up here... It looks like the problem is that the POST request to the web service from Firefox is going as XML, whereas from the others it goes as JSON. As a result, the result is XML, which breaks the template.

I think I reported an issue like this a while ago - what's going on? How come this is still happening with Firefox?

Mike
0
Accepted
Dimitar Terziev
Telerik team
answered on 24 Feb 2014, 09:18 AM
Hi Mike,

As discussed in the support ticket for this same problem, if the json formater is specified in the Global.asax file, the web service should return the data in JSON format which is the format supported by our controls.

Regards,
Dimitar Terziev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
0
Mike
Top achievements
Rank 1
answered on 25 Feb 2014, 03:29 PM
Hi Dimitar,

Thanks for your response and explanation. I didn't really want to remove the XML formatter in the global.asax file, and since this procedure is designed for use with the search box, I just handled it manually. If it helps anybody else, here's the basic code I used to get it working with WebAPI:

Public Class SearchBoxContextDTO
    Property context As SearchBoxContext
End Class
 
<HttpPost()>
Public Function SearchUsersBoxRequest(<FromBody()> postedContext As SearchBoxContextDTO) As HttpResponseMessage 'SearchBoxItemData()
    Dim output As New List(Of SearchBoxItemData)
    Dim resultItem As SearchBoxItemData
    Dim resultData As Dictionary(Of String, Object)
    Dim rsp As HttpResponseMessage = Request.CreateResponse
    Dim serializer As New JavaScriptSerializer
 
'(search logic removed - fills output with results)
 
    If output.Count > 0 Then
        rsp.Content = New StringContent(serializer.Serialize(output.ToArray))
    Else
        rsp.Content = New StringContent("")
    End If
 
    With rsp
        .StatusCode = Net.HttpStatusCode.OK
        .Content.Headers.ContentType = New MediaTypeHeaderValue("application/json")
    End With
 
    Return rsp
End Function

Hope all's well,

Mike
Tags
SearchBox
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Share this question
or