VB.NET 后面的代码没有从 AJAX 请求中获取数据
VB.NET code behind doesn't get the data from AJAX request
如果你能帮助我,我会很高兴。我在 Page_Load 中获取应该来自 AJAX 请求的数据时遇到问题。我做了一个 AJAX 请求:
$.post({
url: 'pdf.aspx',
//data: { ID: '1833' },
data: 'id=1833',
dataType: 'text',
type: 'post',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (data, textStatus, jQxhr) {
console.log("sssss " + data);
},
failure: function (msg) {
console.log("fffff " + msg);
},
error: function (jqXhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
我有代码在后面:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim ID = Request.Form("id")
Response.Write(ID)
End Sub
因此,我对 ID 变量一无所获。我尝试了所有可能的选项,但仍然有问题。有任何想法吗?感谢您的任何回答!
这是你的 aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="VisualBasicWebForm.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(function () {
$("#theBtn").click(function () {
$.post({
url: 'WebForm1.aspx/GetData',
type: 'POST',
//using get all textbox selector-you can use a different selector
data: '{ID: "' + $(":text").attr("ID") + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, jQxhr) {
//don't forget the .d
alert("sssss " + data.d);
},
error: function (jqXhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="passToServer" />
<input type="button" id="theBtn" value="Go" />
</div>
</form>
</body>
</html>
下面是您的代码:
Imports System.Web.Services
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
<WebMethod()>
Public Shared Function GetData(ByVal ID As String) As String
'Dim ID = Request.Form("id")
'Response.Write(ID)
'Instead of the above, you can put a breakpoint on the next line to see value of ID
'Also returning ID so it is display, in your case sent to console.log
Return ID
End Function
End Class
如果你能帮助我,我会很高兴。我在 Page_Load 中获取应该来自 AJAX 请求的数据时遇到问题。我做了一个 AJAX 请求:
$.post({
url: 'pdf.aspx',
//data: { ID: '1833' },
data: 'id=1833',
dataType: 'text',
type: 'post',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (data, textStatus, jQxhr) {
console.log("sssss " + data);
},
failure: function (msg) {
console.log("fffff " + msg);
},
error: function (jqXhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
我有代码在后面:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim ID = Request.Form("id")
Response.Write(ID)
End Sub
因此,我对 ID 变量一无所获。我尝试了所有可能的选项,但仍然有问题。有任何想法吗?感谢您的任何回答!
这是你的 aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="VisualBasicWebForm.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(function () {
$("#theBtn").click(function () {
$.post({
url: 'WebForm1.aspx/GetData',
type: 'POST',
//using get all textbox selector-you can use a different selector
data: '{ID: "' + $(":text").attr("ID") + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, jQxhr) {
//don't forget the .d
alert("sssss " + data.d);
},
error: function (jqXhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="passToServer" />
<input type="button" id="theBtn" value="Go" />
</div>
</form>
</body>
</html>
下面是您的代码:
Imports System.Web.Services
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
<WebMethod()>
Public Shared Function GetData(ByVal ID As String) As String
'Dim ID = Request.Form("id")
'Response.Write(ID)
'Instead of the above, you can put a breakpoint on the next line to see value of ID
'Also returning ID so it is display, in your case sent to console.log
Return ID
End Function
End Class