2013-07-02 68 views
0

我有一个WCF服务,我通过JS调用Jquery来调用它。无法从JavaScript调用Wcf服务

的代码看起来是这样的:

IService1.cs:

[ServiceContract] 
public interface IService1 
{ 
    [WebInvoke(Method = "POST", 
    BodyStyle = WebMessageBodyStyle.Wrapped, 
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json)] 
    [OperationContract] 
    User GetUser(string userName, string password); 
} 

Service1.cs:

0:

​​

由IIS托管完成

<%@ ServiceHost Language="C#" Debug="true" Service="MyProjectName.Service1" CodeBehind="Service1.svc.cs" %> 

Web.config文件:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.web> 
    <compilation debug="true"/> 
    <identity impersonate="false" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="EndpBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
           multipleSiteBindingsEnabled="true" /> 
    <services> 
     <service name="MyProjectName.Service1" 
       behaviorConfiguration="ServiceBehavior"> 
     <endpoint address="" 
        behaviorConfiguration="EndpBehavior" 
        binding="webHttpBinding" 
        contract="MyProjectName.IService1" /> 
     </service> 
    </services> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <standardEndpoint name="" /> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 
</configuration> 

在我的Java脚本的网页我打电话GetUser功能类似:

function CallService() { 
    jQuery.ajaxSetup({ 
     error: function (x, e) { 
      if (x.status == 0) { 
       alert('You are offline!!\n Please Check Your Network.'); 
      } else if (x.status == 404) { 
       alert('Requested URL not found.'); 
      } else if (x.status == 500) { 
       alert('Internal Server Error.'); 
      } else if (e == 'parsererror') { 
       alert('Error.\nParsing JSON Request failed.'); 
      } else if (e == 'timeout') { 
       alert('Request Time out.'); 
      } else { 
       alert('Unknow Error.\n' + x.responseText); 
      } 
     } 
    }); 
    var request = { userName: "aaa", password: "123" }; 
    var jsondata = JSON.stringify(request); 

    $.ajax({ 
     type: "POST", //GET or POST or PUT or DELETE verb 
     url: "http://localhost:xxxx/Service1.svc/GetUser", // Location of the service 
     data: jsondata, //Data sent to server 
     contentType: "application/json; charset=utf-8", // content type sent to server 
     dataType: "json", //Expected data format from server 
     processdata: true, //True or False 
     crossDomain: true, //True or False 
     success: function (result) { 
      alert('success'); 
     } 
    }); 
} 

虽然我可以在浏览器提高服务,我不断收到错误:

You are offline!! Please Check Your Network.

我找不到任何东西来帮助我解决问题,任何人都有想法?

+0

http://stackoverflow.com/questions/872206/http-status-code-0-what-does-this-mean-in-ms-xmlhttp请参阅本 – Bharath

回答

2

我认为你的问题是你的WS不允许跨域访问。

你可以尝试添加下面的代码到你的的Web.config

<configuration> 
    <system.webServer>  
     <httpProtocol> 
      <customHeaders> 
       <add name="Access-Control-Allow-Origin" value="*" /> 
       <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" /> 
       <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
      </customHeaders> 
     </httpProtocol> 
    </system.webServer> 
    <!-- Rest of your code --> 
</configuration> 
+0

我试过了。还是一样的错误。 –

+0

@HodayaShalom你使用的是什么版本的IIS? – maqjav

+0

我该如何检查? –

1

不知道,如果你尝试,但使用JSONP跨站点脚本。
这是我在网上找到的一个例子。 XSS-WCF-From-JQuery

Using JSONP is one technique to get around the limitations imposed by the same origin policy. JSONP has its pros and cons. Spend some time to research other methods that might better benefit your project requirements.

+0

如果我将DataType更改为dataType:“jsonp”,则不会涉及成功函数,也不会涉及错误函数。 –

+0

@HodayaShalom - 呃,并不那么简单。您的服务器需要知道如何处理jsonp请求,并且需要知道如何构建适当的响应。这里(http://blog.jonathanroussel.com/2010/01/jsonp-your-aspnet-web-service-direction.html)是一个可以用来生成jsonp响应的HttpModule示例。 (在这个例子中,他使用asmx WS,但是我确定你会改变什么是必要的)。 –

+0

仍然不明白为什么它无法使用JSON。就像现在一样。 –