Jquery VB.NET代码隐藏不';无法从AJAX请求中获取数据

Jquery VB.NET代码隐藏不';无法从AJAX请求中获取数据,jquery,asp.net,ajax,vb.net,asp.net-ajax,Jquery,Asp.net,Ajax,Vb.net,Asp.net Ajax,如果你能帮助我,我会很高兴的。我在页面加载中获取来自AJAX请求的数据时遇到问题。我执行一个AJAX请求: $.post({ url: 'pdf.aspx', //data: { ID: '1833' }, data: 'id=1833', dataType: 'text', type: 'post', contentType: 'applicat

如果你能帮助我,我会很高兴的。我在页面加载中获取来自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
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
因此,我没有得到任何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>

$(函数(){
$(“#theBtn”)。单击(函数(){
美元邮政({
url:'WebForm1.aspx/GetData',
键入:“POST”,
//使用get all文本框选择器,您可以使用其他选择器
数据:“{ID:”+$(“:text”).attr(“ID”)+“}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:函数(数据、文本状态、jQxhr){
//别忘了那本书
警报(“SSS”+data.d);
},
错误:函数(jqXhr、textStatus、errorshown){
console.log(错误抛出);
}
});
})
})
以下是您的代码:

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
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
导入System.Web.Services
公共类WebForm1
继承System.Web.UI.Page
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
端接头
公共共享函数GetData(ByVal ID作为字符串)作为字符串
'Dim ID=Request.Form(“ID”)
'响应.写入(ID)
'您可以在下一行中放置断点来查看ID的值,而不是上面的值
'还将返回ID以便显示,在您的情况下,将其发送到console.log
返回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>

$(函数(){
$(“#theBtn”)。单击(函数(){
美元邮政({
url:'WebForm1.aspx/GetData',
键入:“POST”,
//使用get all文本框选择器,您可以使用其他选择器
数据:“{ID:”+$(“:text”).attr(“ID”)+“}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:函数(数据、文本状态、jQxhr){
//别忘了那本书
警报(“SSS”+data.d);
},
错误:函数(jqXhr、textStatus、errorshown){
console.log(错误抛出);
}
});
})
})
以下是您的代码:

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
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
导入System.Web.Services
公共类WebForm1
继承System.Web.UI.Page
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
端接头
公共共享函数GetData(ByVal ID作为字符串)作为字符串
'Dim ID=Request.Form(“ID”)
'响应.写入(ID)
'您可以在下一行中放置断点来查看ID的值,而不是上面的值
'还将返回ID以便显示,在您的情况下,将其发送到console.log
返回ID
端函数
末级

type:“POST”,url:“pdf.aspx”,dataType:“json”,data:{id:1833}
top应该是您所需要的全部。我在我刚刚测试和请求的页面上有这种格式(当然有不同的url)。表单(“id”)具有预期的值嗨,谢谢你的回答!我照你说的做了,但我遇到了语法错误:在Qb(jquery.min.js:4)的parse()处,在XMLHttpRequest的A(jquery.min.js:4)处,JSON输入意外结束。(jquery.min.js:4)
type:“POST”,url:“pdf.aspx”,dataType:“json”,data:{id:1833}
up top应该是您所需要的全部。我在我刚刚测试和请求的页面上有这种格式(当然有不同的url)。表单(“id”)具有预期的值嗨,谢谢你的回答!我照你说的做了,但我遇到了语法错误:在Qb(jquery.min.js:4)的parse()处,在XMLHttpRequest的A(jquery.min.js:4)处,JSON输入意外结束。(jquery.min.js:4)谢谢你的回答!不幸的是,我不能在我的项目中使用web方法:(你说“不能”是什么意思使用它们?如果你想要回复,那么这是正确的方法。ajax帖子甚至不会击中你的页面加载处理程序,请参阅以了解原因的解释。我的意思是,在代码隐藏中,我需要创建一个pdf文件并在其中存储数据。如果我记得很清楚,我无法在web方法中使用iTextSharp。可能我错了吗?你为什么不这样做能够从webmethod内部使用iTextSharp?仅仅因为函数是一个web方法并不意味着它不能访问您的项目引用因为web方法是一个静态方法谢谢您的回答!不幸的是,我不能在我的项目中使用web方法:(你说“不能”是什么意思使用它们?如果你想要回复,那么这是正确的方法。ajax帖子甚至不会击中你的页面加载处理程序,请参阅以了解原因的解释。我的意思是,在代码隐藏中,我需要创建一个pdf文件并在其中存储数据。如果我记得很清楚,我无法在web方法中使用iTextSharp。可能我错了吗?你为什么不这样做能够从webmethod内部使用iTextSharp吗?仅仅因为函数是web方法并不意味着它不能访问您的项目引用,因为web方法是静态的