Jquery以使用WCF.错误:XMLHttpRequest无法加载Origin.访问控制允许Origin不允许null

Jquery to consume WCF. error: XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin

本文关键字:Origin 访问控制 加载 null 不允许 WCF 错误 XMLHttpRequest Jquery      更新时间:2023-11-21

我有一个"你好世界"wcf服务:

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>

我有一个非常简单的HTML页面:

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

我收到以下错误:XMLHttpRequest无法加载localhost:49311/Service1.svc/GetData。Access Control Allow Origin 不允许Origin null

有没有更简单的方法来解决这个跨站点/端口的问题?

谢谢!

您的服务器端希望从请求中发送一些来源,但当前它为null,请使用charles或fiddler检查您的请求