Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

I am new to web api. I have created a sample project of WEB API and MVC 4 APP together. Now when I try to make a request as follows I get the error:

#jQuery code.
JavaScript
function GetAllEmployees() {
    jQuery.support.cors = true;
    console.log("Entered to GetAllEmployees");
    $.ajax({
        url: 'http://localhost:51848/api/EmployeeAPI',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            WriteResponse(data);
        },
        error: function (x, y, z) {
            alert(x + '\n' + y + '\n' + z);
        }
    });
}



XMLHttpRequest cannot load http://localhost:51848/api/EmployeeAPI. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:51789' is therefore not allowed access. 



Please guide me:
1. why I am getting this error when both the web api and aap are in one solution.
2. I created the single web api project and hosted it on my local IIS server and still getting the same error.


Thanks :)
Posted

Add the below code in your web.config of your API under <system.webserver> tag

XML
<httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
       <add name="Access-Control-Allow-Headers" value="Content-Type" />
       <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
     </customHeaders>
   </httpProtocol>
   <validation validateIntegratedModeConfiguration="false" />
   <modules runAllManagedModulesForAllRequests="true">
     <remove name="WebDAVModule" />
   </modules>
 
Share this answer
 
if you want use only jQuery, you can create file.php with curl for you other path

in localhost:51789

JavaScript
function GetAllEmployees() {
    jQuery.support.cors = true;
    console.log("Entered to GetAllEmployees");
    $.ajax({
        url: 'http://localhost:51789/EmployeeAPI.php',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            WriteResponse(data);
        },
        error: function (x, y, z) {
            alert(x + '\n' + y + '\n' + z);
        }
    });
}



EmployeeAPI.php

PHP
<?php 

if( $curl = curl_init() ) {
    curl_setopt($curl, CURLOPT_URL, 'http://localhost:51848/api/EmployeeAPI');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    $out = curl_exec($curl);
    echo $out;
    curl_close($curl);
}
?>
 
Share this answer
 
The problem is about same-origin policy[^]. It means that you can not make such call from one domain (localhost:51789) to other domain (localhost:51848).
You may set crossDomain to true on $.ajax, but the only certain solution is set Access-Control-Allow-Origin header on your target domain.
It also possible that you didn't mean to separate the API from the calling application, in which case you have to see why both are on different domains...
 
Share this answer
 
Comments
Manish Kumar Namdev 16-Jun-14 8:48am    
I have put the both application in same solution. Shall I need to make both application's port same? I am not geeting why I am not able to access the API method. I checked in the properties both the application have two different port.
Kornfeld Eliyahu Peter 16-Jun-14 10:04am    
Different port is a different domain as long as we talk about same-origin policy...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900