我正在尝试使用jquery向我的asp.net服务器发送ajax请求并获取一些数据。但是我似乎无法做到这一点。无论我做什么我得到Failed to load resource: the server responded with a status of 500 (Internal Server Error)
错误massege。我知道这是一个重复的问题,但我读了几乎所有我能在stackoverflow和其他网站上读到的东西,并没有一个解决方案帮助了我。我正在尝试调试这个东西并弄清楚我能做什么,但我我不知道我该怎么做呢
这是我的代码:
这是ajax调用:
$(document).ready(function () {
$('.list-item button').on('click', function () {
$.ajax({
type: "POST", //HTTP method
url: "/WebServices/RequestHandler.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{}",
success: function (msg) {
console.log(msg);
},
error: function (xhr, textStatus, thrownerror) {
alert("something went wrong");
console.log(xhr.status);
console.log(xhr.responseText);
console.log(thrownerror);
}
});
});
});
这是我的网络服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class RequestHandler : System.Web.Services.WebService
{
private string connectionString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
[WebMethod]
public static string HelloWorld()
{
return "hello";
}
}
所以你可以看到,这是一个非常简单的方法,我只想弄清楚发生了什么以及为什么它不起作用
是的,我确实省略了这两部分:
contentType: "application/json; charset=utf-8",
dataType: "json",
如所建议的那样,无处不在,但根本没有任何帮助。
这也是错误消息在浏览器控制台中触发时的代码:
<!DOCTYPE html>
<html>
<head>
<title>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</title>
<meta name="viewport" content="width=device-width" />
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
@media screen and (max-width: 639px) {
pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
}
@media screen and (max-width: 479px) {
pre { width: 280px; }
}
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
<br><br>
<b> Exception Details: </b>System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code>
</td>
</tr>
</table>
<br>
<b>Stack Trace:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.]
System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +489329
System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +346
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
</pre></code>
</td>
</tr>
</table>
<br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
</font>
</body>
</html>
<!--
[InvalidOperationException]: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.
at System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)
at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->
我从上面的错误消息中可以理解,网址有问题,它不是"FORMATTED CORRECTLY"
或其他什么,我真的不知道我能做些什么来解决这个错误。
任何想法都将不胜感激
提前致谢
更新 我还读到了默认情况下禁用GET和POST请求的地方,所以我还将这些协议添加到我的Web.config文件中:
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
但我仍有同样的问题
答案 0 :(得分:0)
如果要使用GET调用WebMethod,则需要添加ScriptMethod属性以允许它:
[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public static string HelloWorld()
{
return "hello";
}
如果方法未使用ScriptMethodAttribute标记,则该方法将 通过使用HTTP POST命令调用,响应将是 序列化为JSON
或者,使用POST访问WebMethod。
答案 1 :(得分:0)
好的,所以我终于明白了,事实证明,这是我的.net框架的一个问题。我使用本教程重新安装在我的机器上安装的.net框架,重新启动我的电脑,省略了 static
声明我的webmethod
,现在一切正常