title: Breeze executeQuery Q promise fails CORS
body: <p>I'm having a problem with consuming OData Services using Breeze, I set up a Web API OData service by following <a href="http://briannoyes.net/CommentView,guid,c66c5a98-384c-4abf-b11d-2b1993b041cf.aspx#commentstart" rel="nofollow noreferrer">this guide</a>, from Fiddler it works excellent as expected, but when I try to use it with breeze it fails and gives an error message of "OK":</p>
  
  <pre><code>[Q] Unhandled rejection reasons (should be empty):Error: OK
  </code></pre>
  
  <p>Using fiddler I see it goes and queries for metadata and then it queries for the entities which are returned correctly, what could be the problem here?</p>
  
  <pre><code>breeze.config.initializeAdapterInstances({ dataService: "OData" });
  var manager = new breeze.EntityManager(serverAddress);
  
  var query = new breeze.EntityQuery.from("Laboratories");
  
  manager.executeQuery(query).then(function (data) {
      ko.applyBindings(data);
  }).fail(function (e) {
      alert(e);
  });
  </code></pre>
  
  <p>I enabled CORS by using the nightly build of <a href="https://aspnetwebstack.codeplex.com/wikipage?title=CORS%20support%20for%20ASP.NET%20Web%20API&amp;referringTitle=Specs" rel="nofollow noreferrer">ASP.NET Web API CORS support</a>, it all works fine and I can retrieve the entities since I can see in fiddler that they are returned ... it's just that it doesn't go to the then promise instead it lands in fail.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>In response to @Ward testing from newly created projects I did the following:</p>
  
  <p><strong>PROJECT 1</strong></p>
  
  <p>Created a Web API Project.</p>
  
  <p>Added Microsoft ASP.MET Web API Cross-Origin Resource Sharing (CORS) Reference from Nuget.</p>
  
  <p>Added the following Controller:</p>
  
  <pre><code>namespace CORSBreezeTest1.Controllers
  {
      public class ValuesController : EntitySetController&lt;Value, int&gt;
      {
          ValuesDbContext _context = new ValuesDbContext();
  
          [Queryable]
          public override IQueryable&lt;Value&gt; Get()
          {
              return _context.Values;
          }
  
          protected override Value GetEntityByKey(int key)
          {
              return _context.Values.Find(key);
          }
  
          protected override Value CreateEntity(Value entity)
          {
              Value value = _context.Values.Find(entity.Id);
              if (value != null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Conflict));
              }
              _context.Values.Add(entity);
              _context.SaveChanges();
              return entity;
          }
  
          protected override int GetKey(Value entity)
          {
              return entity.Id;
          }
  
          protected override void Dispose(bool disposing)
          {
              _context.Dispose();
              base.Dispose(disposing);
          }
      }
  }
  </code></pre>
  
  <p>And the following Code First Database:</p>
  
  <pre><code>namespace CORSBreezeTest1
  {
      public class ValuesDbContext : DbContext
      {
          public ValuesDbContext()
              : base("DefaultConnection")
          {
  
          }
  
          public DbSet&lt;Value&gt; Values { get; set; }
      }
  
      public class Value
      {
          [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int Id { get; set; }
  
          public string Name { get; set; }
  
          public int Quantity { get; set; }
      }
  }
  </code></pre>
  
  <p>Added the following lines in <code>WebApiConfig.cs</code></p>
  
  <pre><code>public static void Register(HttpConfiguration config)
  {
  
   // Default code left out here ...
  
   config.Routes.MapODataRoute("Values", "odata", GetEdmModel());
   config.EnableQuerySupport();
   config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
  
  }
  
  private static IEdmModel GetEdmModel()
  {
      ODataModelBuilder builder = new ODataConventionModelBuilder();
      builder.Namespace = "CORSBreezeTest1";
      builder.EntitySet&lt;Value&gt;("Values");
      return builder.GetEdmModel();
  }
  </code></pre>
  
  <p><strong>PROJECT 2</strong>
  Then created another Web API Project.</p>
  
  <p>Added Breeze for ASP.NET Web API Projects Nuget Package</p>
  
  <p>Added datajs Nuget Package.</p>
  
  <p>Added the following lines of code to <code>Index.cshtml</code>:</p>
  
  <pre><code>&lt;p data-bind="visible: !results"&gt;Fetching data ... &lt;/p&gt;
  &lt;ul data-bind="foreach: results, visible: results" style="display: none"&gt;
      &lt;li&gt;
          &lt;span data-bind="text: Name"&gt;&lt;/span&gt;
          &lt;span data-bind="text: Quantity"&gt;&lt;/span&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  
  
  @section Scripts {
      &lt;script src="~/Scripts/knockout-2.2.0.debug.js"&gt;&lt;/script&gt;
      &lt;script src="~/Scripts/q.js"&gt;&lt;/script&gt;
      &lt;script src="~/Scripts/datajs-1.1.0.js"&gt;&lt;/script&gt;
      &lt;script src="~/Scripts/breeze.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          $(function () {
              breeze.config.initializeAdapterInstances({ dataService: "OData" });
              var manager = new breeze.EntityManager("http://serverAddress/odata")
              var query = new breeze.EntityQuery.from("Values");
              manager.executeQuery(query).then(function (data) {
                  ko.applyBindings(data);
              }).fail(function (e) {
                  alert(e);
              });
          });
      &lt;/script&gt;
  }
  </code></pre>
  
  <p>Tested as is and it worked since both websites are on localhost.</p>
  
  <p>Published PROJECT 1 to a web server so that the test will actually see different origins, and tested.</p>
  
  <p><img src="https://i.stack.imgur.com/7wbNB.png" alt="enter image description here"></p>
  
  <p>And this is what Nugget saw:</p>
  
  <p><img src="https://i.stack.imgur.com/A6ige.png" alt="enter image description here"></p>
  
  <p>The first request headers are OPTIONS</p>
  
  <pre><code>OPTIONS /odata/Values HTTP/1.1
  </code></pre>
  
  <p>And the second request headers are GET</p>
  
  <pre><code>GET /odata/Values HTTP/1.1
  </code></pre>
  
  <p>And if I change my <code>fail</code> code to:</p>
  
  <pre><code>fail(function (e) {
   ko.applyBindings(e.body.value);
  });
  </code></pre>
  
  <p>And my knockout code to:</p>
  
  <pre><code>&lt;p data-bind="visible: !$data"&gt;Fetching data ... &lt;/p&gt;
  &lt;ul data-bind="foreach: $data, visible: $data" style="display: none"&gt;
      &lt;li&gt;
          &lt;span data-bind="text: Name"&gt;&lt;/span&gt;
          &lt;span data-bind="text: Quantity"&gt;&lt;/span&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Voila! It came through with the data:</p>
  
  <p><img src="https://i.stack.imgur.com/bcU0w.png" alt="enter image description here"></p>
  
  <p>And this is what the Console saw:</p>
  
  <pre><code>SEC7118: XMLHttpRequest for http://serverAddress/odata/$metadata required Cross Origin Resource Sharing (CORS). 
  localhost:53317
  SEC7119: XMLHttpRequest for http://serverAddress/odata/$metadata required CORS preflight. 
  localhost:53317
  SEC7118: XMLHttpRequest for http://serverAddress/odata/Values required Cross Origin Resource Sharing (CORS). 
  localhost:53317
  SEC7119: XMLHttpRequest for http://serverAddress/odata/Values required CORS preflight. 
  localhost:53317
  [Q] Unhandled rejection reasons (should be empty):Error: OK 
  </code></pre>
  
  <p><strong>PROJECTS 1 &amp; 2</strong> using the <code>BreezeControllerAttribute</code></p>
  
  <p>If I in another test add a new controller following <a href="http://www.breezejs.com/documentation/start-nuget" rel="nofollow noreferrer">Breeze Nuget example</a> and add Breeze for ASP.NET Web API project Nuget package and add the following controller:</p>
  
  <pre><code>namespace CORSBreezeTest1.Controllers
  {
      [BreezeController]
      public class BreezeValuesController : ApiController
      {
          readonly EFContextProvider&lt;ValuesDbContext&gt; _context =
              new EFContextProvider&lt;ValuesDbContext&gt;();
  
          [HttpGet]
          public string Metadata()
          {
              return _context.Metadata();
          }
  
          [HttpGet]
          public IQueryable&lt;Value&gt; Values()
          {
              return _context.Context.Values;
          }
  
          [HttpPost]
          public SaveResult SaveChanges(JObject saveBundle)
          {
              return _context.SaveChanges(saveBundle);
          }
  
          protected override void Dispose(bool disposing)
          {
              base.Dispose(disposing);
          }
      }
  }
  </code></pre>
  
  <p>And then modify the client as following:</p>
  
  <pre><code>//breeze.config.initializeAdapterInstances({ dataService: "OData" });
  var manager = new breeze.EntityManager("http://serverAddress/breeze/BreezeValues")
  </code></pre>
  
  <p>Then the requests change:</p>
  
  <p><img src="https://i.stack.imgur.com/TUFrs.png" alt="enter image description here"></p>
  
  <p>And everything works ... I'm not sure if in part is something that <code>EntitySetController</code> handles requests differently or is Breeze making different requests when changing the <code>dataService</code>.</p>