Search code examples
c#jqueryajaxwcf

Could not add Access-Control-Allow-Origin to my WCF library Project


I'm trying to understand why this ajax called doesn't work

 $.ajax({
        type: 'GET',
        url: "http://localhost:8732/Design_Time_Addresses/InMotionGIT_NT.Address.Service/AddressService/json/capitalize",
        data: { streetAddress : JSON.stringify(streetAddress) , consumer :  JSON.stringify(consumer)} ,
        datatype: "jsonp",
        success: function (data) {
            $('body').append('<div>'+data.IDblah+' '+ data.prueba+'</div>');
            alert(data.IDblah);
        }

The service receive the data is correctly received and the response it's correct. Why am I doing wrong?

I tried adding this property to the ajax called but without success crossDomain : true

[OperationContract()]
[WebInvoke(Method="GET", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
public string Capitalize(StreetAddress streetAddress,ConsumerInformation consumer)

The error that i'm getting it's the common

 XMLHttpRequest cannot load Origin http://localhost:50816 is not allowed by Access-Control-Allow-Origin.

UPDATE

I tried to add the header to the response by adding the configuracion in my App.config file but without success

<system.webServer>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
  </customHeaders>
</httpProtocol>
</system.webServer>

Solution

  • This link would help: http://enable-cors.org/

    You need to add the following headers in your response that is sent back to the client:

    //Allow all domains

    Access-Control-Allow-Origin: *

    OR

    //Allow specific domains

    Access-Control-Allow-Origin: http://example.com:8080 http://foo.example.com