Dip Ghosh
Dip Ghosh

Reputation: 21

JSON error while consuming OData with WCF data services

I am using Visual Studio 2012. I have created one Empty Web Application project named ODataService and added one SQL Server Compact 4.0 Local Database and then added one ADO.NET Entity Data Model mapped with the local database. Now added one WCF Data Service which uses the Entity Model. All these are under one single project i.e. ODataService. After creating this I can easily query the data from the browser:

i.e. http://localhost:52964/EmployeeService.svc/Employees()?$filter=ID gt 3

Now I have added one MVC3 application under same solution where I have the ODataService project. In that MVC3 project I have coded the following in the Index.cshtml, to consume odata using jquery:

<script type="text/javascript">
$(document).ready(function () {
var qry = "http://localhost:52964/EmployeeService.svc/Employees?$filter=ID gt 3&$callback=callback&$format=json";
    var qry2 = "http://odata.netflix.com/v2/Catalog/Titles?$filter=ReleaseYear gt 2012&AverageRating gt 4&$callback=callback&$format=json";
    $('#loadData').click(function () {
        $('#loadingText').text('loading data...');


        $.ajax({
            dataType: "jsonp",
            url: qry,
            jsonpCallback: "callback",
            success: callback,
            error: function (jqXHR, textStatus, errorThrown) {
                debugger;
            }

        });

    });

    function callback(result) {
        debugger;
    }
});

When I am using qry2 my code is running good, but when using qry the debugger is hitting in the error section and shows "parsererror" - "callback was not called".

Any kind of help is appreciated.

Upvotes: 2

Views: 676

Answers (1)

Turker
Turker

Reputation: 619

Did you add support for JSONP in your data service? You can see http://archive.msdn.microsoft.com/DataServicesJSONP for the instructions on how to do that.

A much better option may be using the WCF Data Services 5.1.0-rc2 which adds in-the-box support for $format/$callback: http://blogs.msdn.com/b/astoriateam/archive/2012/09/26/wcf-data-service-5-1-0-rc2-released.aspx

Upvotes: 3

Related Questions