如何使用jQuery.ajax [英] how to use jQuery.ajax

查看:76
本文介绍了如何使用jQuery.ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对jQuery非常陌生,并尝试使用jQuery.ajax做简单的例子
但这每次都向我显示以下消息

"Microsoft JScript运行时错误:"myMethod"未定义"

请指导我缺少的地方.


我有以下HTML代码.

Hello Everyone,

I am very new to jQuery and trying to do simple examples using jQuery.ajax
but every time this is showing me the following message

"Microsoft JScript runtime error: ''myMethod'' is undefined"

Please guide me where I am lacking.


I have the following HTML code.

<input type="button" onclick="callServer()" value="Test Button" />
    <asp:Label ID="lblTest" runat="server">



服务器端代码



the server side code

[WebMethod]
    public void myMethod()
    {
        lblTest.Text = DateTime.Now.ToString();
    }



jQuery代码



the jQuery code

function callServer() {
    $.ajax(
    {
    url: "~/jQuery.aspx.cs/"+myMethod,
    contenType:"application/json; charset=utf-8",
    dataType :"json",
    success:onSuccess,
    fail:onFail
    });
}

function onSuccess() {
    alert("Success!!");
}
function onFail() {
    alert("Failed!!");
}

推荐答案

.ajax( { 网址:〜/jQuery.aspx.cs/" + myMethod, contenType:"application/json; charset = utf-8", dataType:"json", 成功:成功, 失败:失败 }); } 函数onSuccess(){ alert(成功!"); } 函数onFail(){ alert(失败!"); }
.ajax( { url: "~/jQuery.aspx.cs/"+myMethod, contenType:"application/json; charset=utf-8", dataType :"json", success:onSuccess, fail:onFail }); } function onSuccess() { alert("Success!!"); } function onFail() { alert("Failed!!"); }


看看下面有关此主题的提示/技巧文章.(不是广告:))
简化Asp.Net Core Ajax

更新-或者,尝试如下更改网址.
url: "~/jQuery.aspx/myMethod",
Have a look at my Tips/Tricks Article on this topic below.(not an advertisement :))
Simplifying Asp.Net Core Ajax

Update - Alternatively, try by changing your url as below.
url: "~/jQuery.aspx/myMethod",




您必须在代码中进行一些修改

Hi,

you have to change some little bit in your code

function callServer() {


这篇关于如何使用jQuery.ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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