这是我宁静的网络服务代码。我试图从网站上获取一些数据。但是发生了一个错误。它说“网络错误:405方法不允许”我可以做些什么来防止这种情况。
namespace WebService1
{
[System.Web.Script.Services.ScriptService]
[WebService(Namespace = "http://microsoft.com/webservices/")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string sayHelloJson(string name)
{
string strgreeting = string.Format("Hello {0}", name);
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(strgreeting);
}
}
}
这是我的html代码。我使用wamp服务器运行它..
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script>
<script type="text/javascript">
function Greeting(){
var Name=$('#txtname').val();
$.ajax({
type: "POST",
url: 'http://localhost:54884/Service1.asmx?op=ssayHelloJson',
//data: ({ name : 'ggg' }),
dataType: "html",
contentType: "application/x-www-form-urlencoded",
success: function(data) {
// Run the code here that needs
// to access the data returned
alert( data);
},
error: function() {
alert('Error occured');
}
});
}
</script>
</head>
<body>
<form id="form1" >
<div>
<p>json format call</p>
Enter name:<input type="text" id="txtname" />
<input type="button" id="btngo" value="GO" onclick="Greeting()" />
</div>
<p id="result"></p>
</form>
</body>
</html>
答案 0 :(得分:0)
<script type="text/javascript">
function Greeting(){
var Name=$('#txtname').val();
$.ajax({
type: "POST",
//your method name in webservice change like this
url: 'http://localhost:54884/Service1.asmx/ssayHelloJson',
data: ({ name : 'ggg' }),
dataType: "POST",
contentType: "application/x-www-form-urlencoded",
success: function(data) {
// Run the code here that needs
// to access the data returned
alert( data);
},
error: function() {
alert('Error occured');
}
});
}
</script>
你的web配置添加服务获取和发布请求
<location path="Service1.asmx">
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</location>