![](/img/trans.png)
[英]Ajax request for asp.net mvc controller returning 500 - internal server error
[英]ASP.NET VB WebService request with AJAX 500 error
我正在嘗試在 VB ASP.NET 頁面上運行AJAX
Webservice
請求。
當頁面加載時,我正在嘗試調用 web 服務,但在控制台中出現500
錯誤。
我的 WebService 文件如下所示:
<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>
Public Class usrDataSave
Inherits System.Web.Services.WebService
<WebMethod()>
Public Function saydata(abc As String)
MsgBox(abc)
Return abc
End Function
我的 ASP.NET 頁面如下所示:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "usrDataSave.asmx/saydata",
data: "hello_world",
contentType: "application/json",
datatype: "json",
success: function(responseFromServer) {
alert(responseFromServer.d)
}
});
});
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
我希望頁面加載並彈出一個消息框,顯示“hello_world”以及 web 瀏覽器創建一個顯示相同的彈出窗口。 但是,這不會發生,因為我收到了 500 錯誤。
我嘗試通過使用不同版本的jQuery
以及在web.config
文件中啟用請求來解決此問題,如下所示:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
這不起作用,我仍然在 web 瀏覽器控制台中得到“服務器響應狀態為 500”。 應用程序的調試控制台中不會記錄任何錯誤。
我怎樣才能解決這個問題?
好的,假設兩個頁面都在同一個文件夾中 - 在同一級別?
那么這應該工作:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: usrDataSave.asmx/saydata
data: "{abc: 'hello_world'}",
contentType: "application/json",
datatype: "json",
success: function (responseFromServer) {
alert(responseFromServer.d)
}
});
});
</script>
請注意您的數據必須如何匹配您的參數。
所以,假設你有這個:
<WebMethod()>
Public Function saydata(abc As String, def as string) as string
MsgBox(abc)
Return abc & " " & def
End Function
並注意我們如何將 function 設置為字符串 - 你應該給 function 一個類型 - 在本例中為“字符串”。
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "WebService1.asmx/saydata",
data: "{abc: 'hello', def: 'world'}",
contentType: "application/json",
datatype: "json",
success: function (responseFromServer) {
alert(responseFromServer.d)
}
});
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.