Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have developed a .Net Webapi which uses Entity Framework and all is functioning correctly using the uri... //mywebserver/webappfolder/api/mycontroller?val=1&val=2&val=3

The EF and API are in the same project.

With no custom client application necessary at this point, entering this uri into my local PC browser I get the JSON or XML results as expected. So I assumed I could start writing a client to consume the WebAPI on my development box and run it on "//mydeveloperserver/..."

$(document).ready(function () {
           $('#button').click(function () {
               alert('Hello. This works');
               $.get('http://mywebserver/webappfolder/api/mycontroller', function (data) {
                   $.each(data, function (n, val) {
                       $('#OutputDiv').add(n + ':Dedication ' + val.DedicationName);
                   });

               }); 
           });
       });

The JQuery/AJAX was not able to fill the results of the callback function even though I could see the request successfully being made and returned using Fiddler.

After digging through documentation I realized the client consuming app had to run on the same web server "//mywebserver/..." as the WebAPI and once the JQuery/AJAX client web app was moved to "mywebserver" the callback function populated the JSON results as expected. The above code now works

We have website apps on other servers and .Net Windows Forms apps that I want to use to call the WebAPI and consume that data without compromising security. How do I call the WebAPI running on my production server from my developer box web applications. Do I need to have another layer of code on the WebAPI server to allow me to consume that data from elsewhere?

share|improve this question
1  
you have to enable CORS support in your webapi code –  Ajay Beniwal Jul 17 '13 at 8:08

1 Answer 1

As per Ajay's comment you need to enable CORS. To do this download the following Nuget package (N.B. this is a pre-release/beta but it is from the Microsoft product team).

For background information and to read more about how to use this package the manual is here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.