Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过Javascript使用WCF服务_C#_Javascript_Asp.net_Json_Wcf - Fatal编程技术网

C# 通过Javascript使用WCF服务

C# 通过Javascript使用WCF服务,c#,javascript,asp.net,json,wcf,C#,Javascript,Asp.net,Json,Wcf,我创建了一个WCF服务,该服务应该由客户端的Javascript(Json)使用。网站由ASP.NET MVC生成。我在本地主机IIS Express上运行了所有内容。 问题是,当我从Javascript调用这个WCF服务时,它抛出了一个未定义的错误。 出现此错误的原因是,WCF web.config设置不正确,不允许跨站点脚本编写 这是web.config文件 <?xml version="1.0"?> <configuration>

我创建了一个WCF服务,该服务应该由客户端的Javascript(Json)使用。网站由ASP.NET MVC生成。我在本地主机IIS Express上运行了所有内容。 问题是,当我从Javascript调用这个WCF服务时,它抛出了一个未定义的错误。 出现此错误的原因是,WCF web.config设置不正确,不允许跨站点脚本编写

这是web.config文件

  <?xml version="1.0"?>
       <configuration>
         <system.serviceModel>        
           <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <service behaviorConfiguration="Default"
            name="Wcf_Categories.Categories">
        <endpoint address=""
                  behaviorConfiguration="webBehavior"
                  binding="webHttpBinding"
                  contract="Wcf_Categories.ICategories" />    
        <endpoint address="sc"
                  behaviorConfiguration="script"
                  binding="webHttpBinding" 
                  bindingConfiguration="crossDomain"
                  contract="Wcf_Categories.ICategories"/>    
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding"
                        address="mex" />    
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
        <behavior name="script">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>      
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <system.web>
    <compilation debug="true"/>
  </system.web>
</configuration>

如果我直接从浏览器调用服务,例如localhost:1813/Categories.svc/sc/GetData?name=undefined 它返回建议的数据并正常运行(在localhost/TestSites/Products上运行的站点)

下面是wcf服务调用的脚本

function GetCategories(categoryName) {
$.ajax({
    type: "GET",
    async: "false",
    url: "localhost:1813/Categories.svc/sc/GetData",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data : 'name='+categoryName,
    processData: true,
    success: function (result) {
        ServiceSucceeded(result);
    },
    error: ServiceFailed
});
 }

function ServiceSucceeded(result) {
 var resultObject;
 if (DataType == "json") {
    resultObject = result.GetUserResult;    
    for (i = 0; i < resultObject.length; i++) {
        alert(resultObject[i]);
    }    
   }    
 }

function ServiceFailed(xhr) {
        alert(xhr.responseText);<---------------------- Alert output is "undefined". Exception details = [Exception... "<no message>" nsresult: "0x805e0006 (<unknown>)" location: "JS frame :: http://localhost/TestSites/content/js/jquery-1.10.2.min.js :: .send :: line 6" data: no] ------->
if (xhr.responseText) {
    var err = xhr.responseText;
    if (err)
        error(err);
    else
        error({ Message: "Unknown server error." });
}

return;
}
函数GetCategories(categoryName){ $.ajax({ 键入:“获取”, 异步:“假”, url:“localhost:1813/Categories.svc/sc/GetData”, contentType:“应用程序/json;字符集=utf-8”, 数据类型:“json”, 数据:“名称=”+类别名称, processData:对, 成功:功能(结果){ 服务成功(结果); }, 错误:服务失败 }); } 函数ServiceSuccessed(结果){ var结果对象; 如果(数据类型==“json”){ resultObject=result.GetUserResult; 对于(i=0;i脚本的一个潜在问题是,
数据
不是应用程序/json类型,因为declare如果您的WCF和web服务器位于不同的端口上,这将是一个跨域问题,是的。