JQuery调用WCF(非宿主IIS服务)的问题,显示Method not allowed.

yespie 2012-01-31 01:33:25
我想写个WCF,不想布署在IIS上,想用windows服务或宿主在WPF里。现在的问题是用GET方式求请WCF没有问题,但是用POST方式就有问题,先说一下代码是从网上搜来的,我做了一些小小的改动,代码大概如下:

注:
我在IE中用这个地址访问:http://127.0.0.1:3721/employees/InsRow
会显示:Method not allowed.





//IEmployees.cs

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;

namespace Artech.WcfServices.Service.Interface
{
[ServiceContract]
public interface IEmployees
{
[WebGet(UriTemplate = "all",ResponseFormat =WebMessageFormat.Json)]
IEnumerable<Employee> GetAll();


[OperationContract, WebInvoke(UriTemplate = "InsRow",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string InsertRow(string id, string title, string content);

}

[DataContract]
public class Employee
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public string Grade { get; set; }
}
}








//EmployeesService.cs

using System.Collections.Generic;
using Artech.WcfServices.Service.Interface;

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

namespace Artech.WcfServices.Service
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeesService : IEmployees
{
public IEnumerable<Employee> GetAll()
{
return new List<Employee>
{
new Employee{ Id = "001", Name="张三", Department="开发部", Grade = "G6"},
new Employee{ Id = "002", Name="李四", Department="人事部", Grade = "G7"},
new Employee{ Id = "003", Name="王五", Department="销售部", Grade = "G8"}
};
}


public string InsertRow(string id, string title, string content)
{
return string.Format("您输入的标题是:{0}\n\n您输入的内容是:{1}\n\n此文章的id是:{2}", title, content, id);
}

}
}








//Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;
using System.ServiceModel;
using Artech.WcfServices.Service.Interface;

namespace Artech.WcfServices.Service
{
public class Program
{
static void Main()
{
using (WebServiceHost host = new WebServiceHost(typeof(EmployeesService)))
{
host.Open();
Console.Read();
}
}
}
}







App.config





<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="Artech.WcfServices.Service.EmployeesService">
<endpoint kind="webHttpEndpoint"
address="http://127.0.0.1:3721/employees" behaviorConfiguration="ProductsBehavior"
contract="Artech.WcfServices.Service.Interface.IEmployees"/>


</service>
</services>

<behaviors>
<endpointBehaviors>
<behavior name="ProductsBehavior">
<webHttp />
<!--<enableWebScript />如果需要支持脚本调用请启用此项-->
</behavior>
</endpointBehaviors>

<serviceBehaviors>
<behavior name="ProductsBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>


<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

</system.serviceModel>
</configuration>






Default.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>
<style type="text/css">
body
{
font-size: 12px;
text-align: center;
}
#employees
{
border: 1px solid #000000;
margin: 10px auto;
background-color: #eee;
}
#employees tr
{
line-height: 23px;
}
#employees th
{
background-color: #ccc;
color: #fff;
}
.oddRow
{
background-color: #fff;
}
</style>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "get",
url: "http://127.0.0.1:3721/employees/all",
dataType: "jsonp",
success: function (employees) {
$.each(employees, function (index, value) {
var detailUrl = "detail.html?id=" + value.Id;
var html = "<tr><td>";
html += value.Id + "</td><td>";
html += "<a href='" + detailUrl + "'>" + value.Name + "</a></td><td>";
html += value.Grade + "</td><td>";
html += value.Department + "</td></tr>";
$("#employees").append(html);
});
$("#employees tr:odd").addClass("oddRow");
}
});

});

function callServer() {
var id = Number($("#id").val());
var title = String($("#title").val());
var content = String($("#content").val());
$.ajax({
cache: false,
async: true,
type: "POST",
dataType: "json",
url: "http://127.0.0.1:3721/employees/InsRow",
data: '{"id":' + id + ',"title":"' + title + '","content":"' + content + '"}',
contentType: "application/json;charset=utf-8",
success: function (msg) {
var a = eval('(' + msg + ')');
if (String(a.d).length > 0) { alert(a.d); }
else { alert("服务器超时"); }
}
});

}


</script>
</head>
<body>
<table id="employees" width="600px">
<tr>
<th>ID</th>
<th>姓名</th>
<th>级别</th>
<th>部门</th>
</tr>
</table>
</p>
<div>
文章标题:<input type="text" id="title" />
<br />
文章内容:<textarea id="content"></textarea>
<input type="hidden" id="id" value="1" /><br />
<input type="button" value="提交" onclick="callServer();" />
</div>

</body>
</html>


...全文
2390 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
btbear 2013-08-20
  • 打赏
  • 举报
回复
引用 5 楼 Sandy945 的回复:
[OperationContract, WebInvoke(UriTemplate = "InsRow", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] ------------- 要写在实现上,不是写在契约上
这个无所谓吧
阿非 2012-03-04
  • 打赏
  • 举报
回复
还没搞定? 上传你的项目到CSDN下载频道 把链接贴下
yespie 2012-03-02
  • 打赏
  • 举报
回复
定解啊
机器人 2012-02-11
  • 打赏
  • 举报
回复
这个是js的跨域问题啊。服务端需要配置

http://www.cnblogs.com/liujiang/archive/2008/11/21/1338277.html
yespie 2012-02-01
  • 打赏
  • 举报
回复
我是用这个js调用的:


function callServer() {
var id = Number($("#id").val());
var title = String($("#title").val());
var content = String($("#content").val());

$.ajax({
cache: false,
async: true,
type: "POST",
dataType: "json",
url: "http://127.0.0.1:3721/employees/InsRow",
data: '{"title":"' + title + '"}',
contentType: "application/json;charset=utf-8",
success: function (msg) {
var a = eval('(' + msg + ')');
if (String(a.d).length > 0) { alert(a.d); }
else { alert("服务器超时"); }
}
});

}
阿非 2012-01-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yespie 的回复:]
谢谢Sandy945,我看了你Blog上WebMessageBodyStyle文章,我现在改动了这两个文件,然后我在IE中用这个地址访问:http://127.0.0.1:3721/employees/InsRow
仍然显示:Method not allowed.
是不是我还有什么地方没改正确,不好意思,有空时,再帮我一把,谢谢!
[/Quote]

在IE中访问是get 方式,你需要延续之前的ajax post 或者 form post 的方式
yespie 2012-01-31
  • 打赏
  • 举报
回复
这两句是不是要去掉
  • 打赏
  • 举报
回复
<serviceMetadata httpGetEnabled="true" />
还有这一句
  • 打赏
  • 举报
回复
<!--<enableWebScript />如果需要支持脚本调用请启用此项-->
我就看到了这一句
yespie 2012-01-31
  • 打赏
  • 举报
回复
谢谢Sandy945,我看了你Blog上WebMessageBodyStyle文章,我现在改动了这两个文件,然后我在IE中用这个地址访问:http://127.0.0.1:3721/employees/InsRow
仍然显示:Method not allowed.
是不是我还有什么地方没改正确,不好意思,有空时,再帮我一把,谢谢!


//IEmployees.cs

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;

namespace Artech.WcfServices.Service.Interface
{
[ServiceContract]
public interface IEmployees
{
[WebGet(UriTemplate = "all",ResponseFormat =WebMessageFormat.Json)]
IEnumerable<Employee> GetAll();


[OperationContract]
string InsertRow(string title);

}

[DataContract]
public class Employee
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public string Grade { get; set; }
}
}




//EmployeesService.cs

using System.Collections.Generic;
using Artech.WcfServices.Service.Interface;

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

namespace Artech.WcfServices.Service
{

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeesService : IEmployees
{
public IEnumerable<Employee> GetAll()
{
return new List<Employee>
{
new Employee{ Id = "001", Name="张三", Department="开发部", Grade = "G6"},
new Employee{ Id = "002", Name="李四", Department="人事部", Grade = "G7"},
new Employee{ Id = "003", Name="王五", Department="销售部", Grade = "G8"}
};
}

[WebInvoke(UriTemplate = "InsRow",
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public string InsertRow(string title)//, string title, string content)
{
return string.Format("您输入的标题是:{0}\n\n", title);
}

}
}
阿非 2012-01-31
  • 打赏
  • 举报
回复
[OperationContract, WebInvoke(UriTemplate = "InsRow",
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
-------------
要写在实现上,不是写在契约上
yespie 2012-01-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ken_flash 的回复:]

BodyStyle = WebMessageBodyStyle.WrappedRequest,

改成BodyStyle = WebMessageBodyStyle.Bare试一下
[/Quote]

谢谢,终于有人回答了



host.Open();
会有异常:
Operation 'InsertRow' of contract 'IEmployees' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.

ken_flash 2012-01-31
  • 打赏
  • 举报
回复
BodyStyle = WebMessageBodyStyle.WrappedRequest,

改成BodyStyle = WebMessageBodyStyle.Bare试一下
yespie 2012-01-31
  • 打赏
  • 举报
回复
先祝各位新年快乐,身体健康,工作愉快!

12,166

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧