Web方法不调用由javascript函数的jQuery函数、方法、Web、jQuery

2023-09-10 19:15:13 作者:δ残魂

在这里很长一段时间的WinForm程序员,但新来的网络编程的场景。我有Visual Studio 2010和我创建了一个新的网站项目。 我似乎无法让AJAX调用我创建一个WebMethod。:当我点击页面没有我的按钮要发生的。

它看起来像jQuery 1.4.1就会自动的脚本文件夹中添加的,当我创建了一个网站项目。

在Default.aspx的我加2脚本标记:

 <脚本SRC =脚本/ jQuery的-1.4.1.js类型=文/ JavaScript的>< / SCRIPT>
< SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的SRC =脚本/ Process.js>
 

我把一个按钮在的onclick功能在Process.js定义的页面上:

 <输入ID =为btnTest类型=按钮值=测试的onclick =btnTest_onclick()/>
 
ThingJS如何实现3D可视化应用开发的

在Process.js我有以下的code:

 函数btnTest_onclick(){

    VAR strData是= JSON.stringify({
        用户名:5
    });

    警报(strData是);

    $阿贾克斯({
        网址:Default.aspx的/的GetData',
        键入:POST,
        数据:strData是,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        成功:成功,
        失败:失败,
        异步:真
    });
}

功能成功(数据){
    警报(成功);
}

功能失效(数据){
    警报(失败);
}
 

在Default.aspx.cs:

 公共部分类_Default:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的)
    {

    }

    [WebMethod的()]
    公共静态字符串的GetData(INT ID)
    {
        返回你好,我的ID是+ ID;
    }
}
 

解决方案

我不知道,如果让使用JQuery Ajax请求是对你的要求,但你可以更轻松地完成Ajax请求使用ASP的能力页面的方法。 NET AJAX。

首先我要注意,要使用你需要添加脚本管理器控件的页面PageMethods和指定的EnablePageMethods的=真。

当你这样做,ScriptManager可以使这将重新在页面上定义的页面方法present代理PageMethods客户端定义,你可以使用Ajax请求轻松地调用此方法。

所以,你可以试试下面的code:

 < ASP:ScriptManager的ID =ScriptManager的=服务器的EnablePartialRendering =真的EnablePageMethods =真/>
<输入ID =为btnTest类型=按钮值=测试的onclick =btnTest_onclick()/>
<脚本类型=文/ JavaScript的>
    传播btnTest_onclick(){

        PageMethods.GetData(5,成功,失败);
    }

    功能成功(数据){
        警报(成功);
        警报(数据);
    }

    功能失效(数据){
        警报(失败);
    }

< / SCRIPT>
 

正如你所看到的这code具有code少线和它的作品。

Long time WinForm programmer here but new to the web programming scene. I have Visual Studio 2010 and I created a new WebSite project. I can't seem to get ajax to call a webmethod I created. When I click my button on the page nothing happens at all.

It looks like jquery 1.4.1 gets automatically added in a Scripts folder when I create a WebSite project.

In Default.aspx I add 2 script tags:

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript" src="Scripts/Process.js">

I put a button on the page in where the onclick function is defined in Process.js:

<input id="btnTest" type="button" value="Test" onclick="btnTest_onclick()" />

In Process.js I have the following code:

function btnTest_onclick() {

    var strData = JSON.stringify({
        userid: 5
    });

    alert(strData);

    $.ajax({
        url: 'Default.aspx/GetData',
        type: "POST",
        data: strData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: Success,
        failure: Failure,
        async: true
    });
}

function Success(data) {
    alert("success");
}

function Failure(data) {
    alert("failure");
}

In Default.aspx.cs:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod()]
    public static string GetData(int id)
    {
        return "hello, my id is " + id;
    }
}

解决方案

I don't know if making Ajax request using JQuery is requirement for you, but you can more easily do ajax request to page methods using abilities of the ASP.NET ajax.

First of all I want to notice that to use PageMethods you need to add script manager control to the page and specify EnablePageMethods="True".

When you will do so, ScriptManager will render PageMethods client side definition which represent proxy of the defined page methods on the page and you can easily invoke this methods using Ajax request.

So, you can try the following code:

<asp:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" EnablePageMethods="True" />
<input id="btnTest" type="button" value="Test" onclick="btnTest_onclick()" />
<script type="text/javascript">
    function btnTest_onclick() {

        PageMethods.GetData(5, Success, Failure);
    }

    function Success(data) {
        alert("success");
        alert(data);
    }

    function Failure(data) {
        alert("failure");
    }

</script>

As you can see this code has less lines of code and it works.

 
精彩推荐
图片推荐