【问题标题】:jquery.ajax calling a .aspx web methodjquery.ajax 调用 .aspx web 方法
【发布时间】:2015-11-04 20:18:46
【问题描述】:

我有以下代码在没有变量的 POC 中工作,并对所有内容进行了硬编码。在引入变量以准备使用此通道后,它停止工作。可能是我没有看到语法错误,或者(我不敢说)这个 POC 不能支持这样的请求?

来自我的 aspx 页面 sendEmail.aspx

     [System.Web.Services.WebMethod]
                public static string SendMyEmail(string EmailFromAddress, string EmailFromName, string EmailSubject, string EmailBody)
                {

                    return "data from server: " + Environment.NewLine +
                            "EmailFromAddress = " + EmailFromAddress + Environment.NewLine +
                            "from = " + EmailFromName + Environment.NewLine +
                            "from = " + EmailSubject + Environment.NewLine +
                            "from = " + EmailBody;

                }

    <script type = "text/javascript">
    function ShowCurrentTime() {
        alert("hi");

        $.ajax({
            type: "POST",
            url: "SendEmail.aspx/SendMyEmail",
            data: '{EmailFromAddress: "mike", EmailFromName="mike", EmailSubject="email subject here", EmailBody="email body here"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }

来自我的 html 网页:

<script type = "text/javascript">
function ShowCurrentTime() {
    alert("hi");

    $.ajax({
        type: "POST",
        url: "SendEmail.aspx/SendMyEmail",
        data: '{EmailFromAddress: "mike", EmailFromName="mike", EmailSubject="email subject here", EmailBody="email body here"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
}

function OnSuccess(response) {
    alert("all good");

    alert(response.d);
}
</script> 
</head>
<body style = "font-family:Arial; font-size:10pt">
<form id="form1" runat="server">
<div>
Your Name : 
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time" 
    onclick = "ShowCurrentTime()" />
</div>
</form>
</body>
</html>

当 web 方法有一个婴儿车(如下所示)时,即使 json 数据中有多个数据点,它也能很好地工作:

[System.Web.Services.WebMethod]
        public static string SendMyEmail(string EmailFromAddress)
        {

            return "good data from server: " + EmailFromAddress;

        }

【问题讨论】:

  • 尝试传入 webmethod 需要的所有变量。您只是发送 EmailFromAddress,请尝试添加其余部分。最有可能只传递 1 个参数,系统正在寻找仅具有该参数的方法。
  • 不行,我原来是这样的:data: '{EmailFromAddress: "mike", EmailFromName="mike", EmailSubject="email subject here", EmailBody="email body here"} ',

标签: jquery asp.net ajax webmethod


【解决方案1】:

你的 ajax 应该是这样的。删除 '=' 并替换为 ':' 以在数据字段中创建正确的 json。

$.ajax({
    type: "POST",
    url: "SendEmail.aspx/SendMyEmail",
    data: '{EmailFromAddress: "mike", EmailFromName: "mike", EmailSubject: "email subject here", EmailBody: "email body here"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function (response) {
        alert(response.d);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多