1

My setup is like this, both servers are in different domains.

  1. SharePoint Foundation 2013 with an HTML/JavaScript app, hosted on Office365 by Microsoft.
  2. Windows Server 2012 with Dynamics NAV 2013, hosted on Azure by Microsoft.

What I like to do is to call a NAV 2013 oData webservice through jQuery. I have checked that my webservice is accessible from the browser, and I do get a successfull response.

But when I try calling it from my app in SharePoint I do get an error saying.

SyntaxError: syntax error <?xml version="1.0" encoding="utf-8" standalone="yes"?>

This is my jQuery code:

var call = $.ajax({
    url: 'http://url:port/DynamicsNAV70_Instance/odata/MyService',
    type: "GET",
    crossDomain: true,
    username: "username",
    password: "password",
    dataType: "jsonp",
    headers: {
        Accept: "application/json;odata=verbose"
    }
});

call.done(function (data, textStatus, jqXHR) {
    console.log(data.d);
});

call.fail(function (jqXHR, textStatus, errorThrown) {
    console.log("Call failed. Error: " + jqXHR.statusText);
});

I have discovered that ´jsonp´ does not work well with XML response, but this call should return json shouldnt it?

Also, in FireBug I can actually see the complete XML returned from the service, and it is completly correct. So my app do get the correct XML, but looks like it is a parsing error?

I also thoughyt about enabling CORS (http://enable-cors.org/index.html) on my Dynamics server, but not sure how I can do this?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Martin at Mennt
  • 5,677
  • 13
  • 61
  • 89

2 Answers2

1

You'll want to check out: http://msdn.microsoft.com/en-us/library/dn127071(v=nav.71).aspx

The way I have seen this done, I would modify the code in the following manner:

jQuery.support.cors = true;
var call = $.ajax({
    url: 'http://url:port/DynamicsNAV70_Instance/odata/MyService?$format=json',
    type: "GET",
    username: "username",
    password: "password",
    dataType: "json",
    headers: {
        Accept: "application/json;odata=verbose"
    }
});
0

I've searched this answer a few times and it has never quite worked for me, possibly because I want to pass through my Windows credentials.

I finally got something to work (NAV2015) after reading this article http://www.telerik.com/blogs/cross-domain-queries-to-odata-services-with-jquery and this page https://msdn.microsoft.com/en-us/library/dn127071(v=nav.80).aspx. The answer seems to be that you need to use jsonp, which is designed to get around cross-domain restrictions.

Hope this helps someone :-)

$.ajax({        
    url: 'http://Server:Port/DynamicsNAV80/OData/Service?$format=json&$callback=?',
    contentType: 'application/json; charset=utf-8',
    dataType: "jsonp",
    type: "GET",
    xhrFields: {
        withCredentials: true
    },
    dataType: "jsonp",
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function (xhr, textStatus, errorMessage) {
        alert('Error: ' + errorMessage);
    }
});

Note: In IE and Chrome this works as I expect - just passing through my credentials. In Firefox I am asked for a login twice, once for my site, once for the service, I don't know if this is my Firefox settings or something I have missed in the Web.config.