ASP.NET Ajax的jQuery的调用code-背后方法 [英] ASP.NET jQuery Ajax Calling Code-Behind Method

查看:192
本文介绍了ASP.NET Ajax的jQuery的调用code-背后方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的非常的新web开发,但有很多的经验,在发展一般。我有一个ASP页有几个输入框和一个提交按钮。这个提交按钮纯粹调用$阿贾克斯,我打算让呼叫在code-隐藏文件的方法。然而,我发现了两个有趣的事情。首先,AJAX调用成功,无论什么样的数据提供给它的。其次,responseText的领域是整个网页的HTML源代码。

I am very new to web development, but have a lot of experience in development in general. I have an ASP page that has a few input fields and a submit button. This submit button purely calls $.ajax, which I intended to have call a method in the code-behind file. However, I've noticed two interesting things. First, the ajax call succeeds regardless of what data is provided to it. Secondly, the responseText field is the entire page's html source.

我读过<一个href="http://stackoverflow.com/questions/583116/call-asp-net-pagemethod-webmethod-with-jquery-returns-whole-page">this而且指向webconfig,但这些解决方案似乎没有其他物品,以解决我的问题。

I've read this and other articles that point to the webconfig, but these solutions do not seem to resolve my issue.

下面是ASP页:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script src="TesScript.js"></script>
    <link rel="Stylesheet" type="text/css" href="TestStyle.css" />
</head>
<body>
    <div>
        <ul class="tempList">
            <li>Name:
                <input id="nameText" type="text" />
            </li>
            <li>Attending:
                <input id="yesButton" type="radio" name="attending" />
                Yes
                <input id="noButton" type="radio" name="attending" />
                No </li>
            <li>Return Address:
                <input id="returnAddressText" type="text" />
            </li>
            <li>
                <input id="submitButton" type="button" onclick="submit()" value="Submit" />
            </li>
        </ul>
    </div>
    <ul id="errorContainer" class="errorSection" runat="server" />
    <ul id="messageContainer" class="messageSection" runat="server" />
</body>
</html>

背后的code:

The code behind:

using System;
using System.Web.Services;
using System.Web.UI;

namespace TestAspStuff
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }


        [WebMethod]
        public static string OnSubmit(string name, bool isGoing, string returnAddress)
        {
            return "it worked";
        }
    }
}

和JavaScript的:

And the JavaScript:

function submit() {

    var name = "my name";
    var isAttending = true;
    var returnAddress = "myEmail@gmail.com";

    SendMail(name, isAttending, returnAddress);
}

function SendMail(person, isAttending, returnEmail) {

    var dataValue = { "name": person, "isGoing": isAttending, "returnAddress": returnEmail };

    $.ajax({
        type: "POST",
        url: "Default.aspx/OnSubmit",
        data: dataValue,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
        },
        complete: function (jqXHR, status) {
            alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
        }
    });

}

现在,我发现我可以在URL属性更改为任何我想要的错误方法不会被调用,并且状态是成功的,与responseText的是整个HTML页面。我的webconfig拥有所有适当的部分(包括htmlModule部分)。我工作在.NET 3.5中。我AP preciate任何帮助,并再次,我真的很新的这有啥明显的给他人是最有可能不是很明显我。如果有更好的方法来做到这一点(调用asp.net $ C $从JavaScript C-背后的方法,那就是),请随意张贴。谢谢!

Now, I noticed I can change the url property to anything I want and the error method is never called, and the status is success, with the responseText being the entire html page. My webconfig has all of the appropriate sections (including the htmlModule section). I am working in .Net 3.5. I appreciate any help and, again, I'm really new to this so what's obvious to others is most likely not obvious to me. And if there's a better way to do this (calling asp.net code-behind methods from JavaScript, that is) please feel free to post that. Thanks!!!

推荐答案

首先,你可能要加一个return FALSE;您提交()方法在JavaScript的底部(因此停止提交,因为你处理它的AJAX)。

Firstly, you probably want to add a return false; to the bottom of your Submit() method in JavaScript (so it stops the submit, since you're handling it in AJAX).

您要连接到的完全的事件,不是的成功的事件 - 有一个显著的差异,这就是为什么你的调试结果并不如预期。另外,我从来没有方法匹配你的签名,我一直提供的contentType和数据类型。例如:

You're connecting to the complete event, not the success event - there's a significant difference and that's why your debugging results aren't as expected. Also, I've never made the signature methods match yours, and I've always provided a contentType and dataType. For example:

$.ajax({
        type: "POST",
        url: "Default.aspx/OnSubmit",
        data: dataValue,                
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (result) {
            alert("We returned: " + result);
        }
    });

这篇关于ASP.NET Ajax的jQuery的调用code-背后方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆