你好我的问题是我使用microsoft visual studio来创建wcf webservice,当我在microsoft visual studio上运行时,一切都很好并且有效。但我必须连接到外面的webservice所以当我这样做它不连接。所以首先它给我crossDomain错误,所以我改变webconfig,我从外部的HTML文件中获取值,但我不能发布。我给了这个错误:
GET http://localhost:3281/UserService.svc/SetUser?callback=jQuery11020891759618…20%22usertype%22:%20%22%22,%20%22email%22%20:%20%22%22%20}&_=1386343306675 405 (Method Not Allowed)
我的英语不好所以我会添加你自己可以看到的代码和源文件。
首先是我的javascript:
<script>
function btnSubmit() {
$.support.corps = true;
$.ajax({
crossDomain: true,
cache: false,
async: false,
type: "POST",
url: "http://localhost:3281/UserService.svc/SetUser",
data: '{ "usertype": "' + $("#txtuserName").val() + '", "email" : "' + $("#txtuserEmail").val() + '" }',
contentType: "application/json;charset=utf-8",
dataType: "jsonp",
success: function (r) { alert("Successfully Registered!!!"); },
error: function (e) { alert(e.statusText); }
});
}
function btnRetrieve() {
$.support.corps = true;
$.ajax({
crossDomain: true,
cache: false,
async: false,
type: "GET",
url: "http://localhost:3281/UserService.svc/GetUser",
data: { name: $("#find").val() },
contentType: "application/json;charset=utf-8",
dataType: "jsonp",
success: function (r) {
if (r != null) $("#DisplayResult").html(r.Email);
else
$("#DisplayResult").html("Bilgi yok");
},
error: function (e) { alert(e.statusText); }
});
}
</script>
我的服务内容:
namespace JQueryCallToWcf
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserService
{
// Create a List of User type and add temporary data. This List will be used a
// Fake Repository for Data Tranasaction. In real world we can replace this with
// EntityFramework or Linq2Sql Data Model for actual data transactions.
public static List<User> lstUsers = new List<User>()
{
new User() { Name="Rami", Email="Rami@Rami.com"},
new User() { Name="Bill", Email="Bill@Bill.com"},
new User() { Name="Mark", Email="Mark@Mark.com"},
};
// We have two service methods - SetUser and GetUser, which sets and gets
// user from fake repository.
[OperationContract]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public void SetUser(string usertype, string email)
{
lstUsers.Add(new User() { Name = usertype, Email = email });
}
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json)]
public User GetUser(string name)
{
User op = lstUsers.Where(p => p.Name == name).FirstOrDefault();
return op;
}
}
// This is the User Class, holding properties for Name and email.
[DataContract]
public class User
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
}
}
我为crossdomain添加了webconfig
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonp" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="JQueryCallToWcf.UserService">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonp"
contract="JQueryCallToWcf.UserService"
behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
</system.serviceModel>
我给你我的解决方案文件它的工作,我也给你外面的html文件得到的是工作帖子没有。
答案 0 :(得分:0)
我使用jsonp并在web.config中更改绑定。它现在正在运作。
您可以这样尝试:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="OPTIONSVerbHandler" />
<add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="bitness32" />
</handlers>
</system.webServer>