Search code examples
odatajaydata

Jaydata and Odata- HTTP Request Failed


I have my own custom server to expose data from an XML file. I can browse through it in whichever browser of my choosing and I can query the data in Fiddler, but Jaydata (or one of its building blocks) can't seem to grab the same data. What's most frustrating is that my code is (or was, I've tweaked it slightly to try and resolve these errors) pretty much an exact duplicate of code found here and here. Here's my script:

<script type="text/javascript" src="/Scripts/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="/Scripts/datajs-1.0.3.js"></script>
<script type="text/javascript" src="/Scripts/jaydata.js"></script>
<script type="text/javascript" src="/Scripts/jaydataproviders/oDataProvider.js"></script>
<script type="text/javascript" src="/Scripts/Block.js"></script>
<script type="text/javascript">
    var Context = new foo({
        name: 'oData',
        oDataServiceHost: 'http://localhost:xxx'
    });

    function createItemLI(user, id, css) {
        var li = "<li></li>".append(name).addClass(css).data('id', id);
        return li;
    }

    $.when($.ready, Context.onReady()).then(function () {
        Context.Roots.toArray(function (roots) {
            roots.forEach(function (root) {
                $('#roots').append(
                    createItemLI(root.User, root.ID, 'root'));
            });
        });
    });
</script>

Block.js, is the file generated by JaySvcUtil.exe There's only one thing in the body of the .htm file, a simple <ul id="roots"></ul>

When I try to run the project, there's nothing on the page. When I used FireBug, I get "HTTP request failed" The requestUri is http://localhost:xxx/Roots, which works when I manually browse to it, but the StatusCode is 0, statusText is the empty string, and so on and so forth. I've looked at Fiddler, at it gets exactly what I expected.

I'm assuming there's some manner of flag that needs to be set, but none of the tutorials I've found have been of any help. It's assumed that it works out of the box, and I too had high expectations getting simple read access would be easy.

UPDATE:

it turns out Internet Explorer has been receiving the appropriate data as JSON, though it still doesn't populate the roots as it should. In FireFox it returns a "501 not implemented" error, because my GET request is being altered to be OPTION. I don't have a web.config file as would a project I started as a WCF service. This is just a console app in Visual Studio 2010. So I guess my question becomes "how do I better specify cross-domain behavior through JayData?"


Solution

  • Try This:

    var oProviderConfig = { name: 'oData', oDataServiceHost: 'http://localhost:xxx/Roots/' ,enableJSONP: false };

    Also you have to enable CORS support from your webservice. If you are using .NET

      <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Max-Age" value="3600" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, MaxDataServiceVersion" />
        <add name="Access-Control-Allow-Methods" value="PUT, POST, GET, DELETE, MERGE, OPTIONS" />
      </customHeaders>
    </httpProtocol>