我是wcf服务和.net的新手。我创建了1个wcf测试服务。如果我把文件放在同一个项目目录中,我能得到ajax的响应。如果我使用url从项目目录的外部调用它,那么我没有得到响应。 这是界面文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Web.Script.Services;
namespace TestService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "helloworld/{name}")]
string Helloworld(string name);
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "testservice")]
Dictionary<string,string> testService();
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "getActiveComplains")]
List<TestService.Tickets> GetActiveComplain();
}
[DataContract]
public class Tickets
{
[DataMember]
public string TicketNo { get; set; }
[DataMember]
public string TicketType { get; set; }
[DataMember]
public string Descriptions { get; set; }
[DataMember]
public string PoleNo { get; set; }
[DataMember]
public string Address { get; set; }
}
}
这是类文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.Entity;
namespace TestService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public string Helloworld(string name)
{
return "Hello World, " + name;
}
public Dictionary<string, string> testService()
{
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("test1", "success for test1");
test.Add("test2", "success for test2");
test.Add("test3", "success for test3");
test.Add("test4", "success for test4");
test.Add("test5", "success for test5");
return test;
}
public List<TestService.Tickets> GetActiveComplain()
{
List<TestService.Tickets> objAllTickets = new List<Tickets>();
DataLayer.lmsTicketEntities objlmsTicketEntities = new DataLayer.lmsTicketEntities(System.Configuration.ConfigurationManager.ConnectionStrings["lmsConnectionString"].ToString().Replace("LMSModel", "TicketModel"));
List<DataLayer.GetActiveTicket> objAllComplainTickets = objlmsTicketEntities.GetAllActiveTicket().ToList<DataLayer.GetActiveTicket>();
foreach (var objTicket in objAllComplainTickets)
{
TestService.Tickets objTempTicket = new TestService.Tickets();
objTempTicket.TicketNo = objTicket.TicketNo;
objTempTicket.TicketType = objTicket.TicketType;
objTempTicket.Descriptions = objTicket.TicketDescriptions;
objTempTicket.PoleNo = objTicket.PoleNo;
objTempTicket.Address = objTicket.Address1;
objAllTickets.Add(objTempTicket);
}
return objAllTickets;
}
}
}
这是配置文件
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<!-- Note: the service name must match the configuration name for the service implementation. -->
<service name="TestService.Service1" behaviorConfiguration="MyServiceTypeBehaviors" >
<!-- Add the following endpoint. -->
<!-- Note: your service must have an http base address to add this endpoint. -->
<endpoint contract="TestService.IService1" binding="webHttpBinding" behaviorConfiguration="webHttpBehavior" bindingConfiguration="webHttpBindingWithJsonP" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp defaultOutgoingResponseFormat="Json"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
如果我在服务项目中将此代码放在同一个
,我会得到一个回应<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.5.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/Service1.svc/getActiveComplains",
success: function (html) {
alert(html);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.message);
console.log(XMLHttpRequest);
}
});
});
</script>
</head>
<body>
</body>
</html>
如果我在项目的一侧调用
,那么在下面的情况下,这是行不通的<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.5.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://localhost:3048/Service1.svc/getActiveComplains",
success: function (html) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
}
});
});
</script>
</head>
<body>
</body>
</html>
请帮帮我。