0

I have a "hello world" wcf service:

public string GetData()
{
    return "Hello world";
}

<system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
 </system.webServer>

And I have a very simple HTML page:

    $.ajax({
        type: "GET",
        url: "http://localhost:49311/Service1.svc/GetData/",
        dataType: "xml",
        success: function () {
            alert("test ok");
        },
        error: function (e) {
            alert("error: " +e.responseText);
        }
    });

I'm getting the following error: XMLHttpRequest cannot load localhost:49311/Service1.svc/GetData. Origin null is not allowed by Access-Control-Allow-Origin

Is there a easier way fixing this cross site/port issue?

Thanks!

Kris
  • 5
  • 6
  • you can enable CORS support for the modern browsers – Arun P Johny Nov 27 '13 at 00:26
  • Thanks Arun, But I think I need it on server side, I cannot set it on every client. – Kris Nov 27 '13 at 05:14
  • the cors is enabled on the server side, see question http://stackoverflow.com/questions/7234599/cors-support-within-wcf-rest-services – Arun P Johny Nov 27 '13 at 05:18
  • Thanks again Arun, tried to look up a few, they seems overly complicated, I just feel there should be simpler solutions, not sure if you know any? thanks!! – Kris Nov 27 '13 at 23:21
  • In ajax to support cross domain requests there are 2 ways CORS and jsonp. You can look up for Same Origin Policy violation and solutions – Arun P Johny Nov 28 '13 at 00:25
  • Hi Arun, I have tried jsonp, but having some problem, I have posted the errors, could you please have a look when you got a chance please: http://stackoverflow.com/questions/20278744/jquery-consume-wcf-cross-domain-what-is-a-valid-format-to-return thanks!! – Kris Nov 29 '13 at 05:27

1 Answers1

0

your server side is expecting some origin to be sent from the request and currently it's getting a null, use charles or fiddler to check your request

Varun Sheth
  • 146
  • 1
  • 12
  • Thanks Varun! So does it mean I have to have some origin sent back to the requester? After I have an origin, what should I do next? Thanks!! – Kris Nov 27 '13 at 05:16
  • it shud go thru if u have the Origin because the code is complaining you dont have a Origin, u dont have to send anything back from the server in terms of headers – Varun Sheth Nov 28 '13 at 06:32