这是我宁静的网络服务代码。我试图从网站获取一些数据。但是发生了一个错误。它说“NetworkError:405 Method Not Allowed”我可以做些什么来防止这种情况发生。
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>