2014-01-27 49 views
2

问题是如何从JQuery访问非aspx服务。从JQuery调用WCF服务不起作用

我曾尝试:

最简单的情况:

合同:

using System.ServiceModel; 
using System.ServiceModel.Web; 

namespace TheService 
{ 
    [ServiceContract] 
    public interface ITheService 
    { 
     [OperationContract] 
     [WebGet(ResponseFormat = WebMessageFormat.Json)] 
     string Get(); 
    } 
} 

的服务:使用系统 ; using System.ServiceModel.Web;

namespace TheService 
{ 
    public class TheService : ITheService 
    { 
     public string Get() 
     { 
      return "Hello, world!"; 
     } 
    } 
} 

托管服务:

class Program 
    { 
     static void Main(string[] args) 
     { 
      Uri httpUrl = new Uri("http://localhost:22334/TheService"); 

      ServiceHost host = new ServiceHost(typeof(TheService), httpUrl); 

      ServiceMetadataBehavior serviceMetaDataBehaviour = new ServiceMetadataBehavior 
      { 
       HttpGetEnabled = true, 
       MetadataExporter = {PolicyVersion = PolicyVersion.Policy15} 
      }; 
      host.Description.Behaviors.Add(serviceMetaDataBehaviour); 

      host.AddServiceEndpoint(typeof(ITheService), new WebHttpBinding(), ""); 

      host.Open(); 

      Console.ReadLine(); 
     } 
    } 

如果我尝试用wcftestclient访问服务,我可以访问数据。

现在我想通过jQuery

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 

    <script> 
     $(document).ready(function() { 

    $.getJSON("http://localhost:22334/TheService/Get", 
    function(data){ 
    alert("Data Loaded: " + data); 
    }); 
}); 

    </script> 
</head> 
<body> 

</body> 
</html> 

我认为应该发生的事情是,我得到一个Hello World回访问它。但事实并非如此。

我想:

$.ajax({ 
    type: "GET", 
    url: "http://localhost:22334/TheService/Get", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     alert(msg); 
    }, 
    error: function(err) { 
     alert(err.toString()); 
     if (err.status == 200) { 
      ParseResult(err); 
     } 
     else { alert('Error:' + err.responseText + ' Status: ' + err.status); } 
    } 
}); 

和所有我得到的是两个警报“[对象]对象”和“错误:状态:0”

所以在这种情况下,它似乎运行到一些错误,但在哪?

我从来没有使用过jQuery,所以有什么地方可以得到一些更有用的错误信息?

我需要在wcf服务中定义其他任何东西吗?

最后,两个项目不应该在相同的应用程序。应该在客户端使用JQuery,并在集中式服务器上运行wcf服务。

如果我改变

ServiceHost host = new ServiceHost(typeof(TheService), httpUrl); 

WebServiceHost host = new WebServiceHost(typeof(ServiceCalculator), httpUrl); 

我可以看到,到达该WCF服务器调试点(文本返回) - 但仍两种错误对话框是显示。

(在浏览器中增加了localhost:22334/TheService/Get返回“Hello World” - 因此我认为这是JQuery/Ajax的问题​​,还是?)


更新:

相应的答复,并http://pranayamr.blogspot.de/2011/06/calling-cross-domain-wcf-service-using.html我增加了以下内容:

设置调试为true:

ServiceDebugBehavior debug = host.Description.Behaviors.Find<ServiceDebugBehavior>(); 

if (debug == null) 
{ 
    host.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true }); 
} 
else 
{ 
    if (!debug.IncludeExceptionDetailInFaults) 
    { 
     debug.IncludeExceptionDetailInFaults = true; 
    } 
} 

改变行为,以ASP的兼容性

for (int i = 0; i < host.Description.Behaviors.Count; i++) 
{ 
    if (host.Description.Behaviors[i] is AspNetCompatibilityRequirementsAttribute) 
    { 
     host.Description.Behaviors.RemoveAt(i); 
     break; 
    } 
} 
host.Description.Behaviors.Add(new AspNetCompatibilityRequirementsAttribute { RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed }); 

允许crossdomainscriptaccess

host.Description.Behaviors.Add(serviceMetaDataBehaviour); 
WebHttpBinding binding = new WebHttpBinding {CrossDomainScriptAccessEnabled = true}; 
host.AddServiceEndpoint(typeof(IServiceCalculator), binding, ""); 

,并试图设置JSONP

$.ajax({ 
    type: "GET", 
    url: "http://localhost:22334/TheService", 
    method: "Get", 
    contentType: "application/json; charset=utf-8", 
    dataType: 'jsonp', 
    success: function(msg) { 
     alert(msg); 
    }, 
    error: function(err) { 
     alert(err.toString()); 
     if (err.status == 200) { 
      ParseResult(err); 
     } 
     else { alert('Error:' + err.responseText + ' Status: ' + err.status); } 
    } 
}); 

现在看来,以某种方式工作。 (如果出现问题,仍然没有真正的错误信息...)(并且似乎也不需要asp部分)

回答