开发者

Problem Ajax with Jquery and ASP.NET

I read alot about this on this forum, but I cant make it work.

I want to use the ajax function on my asp.net web application

So here is the Javascript on VerifMain.aspx

$(document).ready(function () {

//menu()
$("#btnImprimer").click(function () {
    $.ajax({
        type: "POST",
            url: "/VerifMain.aspx/Lol",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert("Good"); 
            },
            error: function (msg) {
                alert(msg);
            }
        });
    });
});

And here is the server code in VerifMain.aspx.vb

Partial Public Class _Default
    Inherits Page
    <WebMethod()> _
    Public Shared Sub Lol()
        //TO DO
    End Sub
End Class

So when I'm trying to call this method, It goes in the error function and the alert is "[object Object]"

I have to use JQuery because where I work the Microsoft Ajax is not installed.

I really need help for this, I don't understand what I do wrong and I'm stuck with ie7 only and almost every websites are blocked.

Thank you!

Have a开发者_StackOverflow社区 nice day!!

EDIT: Hi everyone Thank you for your time!

I fixed it by removing the the partial class.

so now it's only a static web method in the server code and it works.

<WebMethod()> _
Public Shared Sub Lol()
    //TO DO
End Sub

To be honest, I don't understand how it works

but thank you for your fast replies.

This is the best website, I will spend some free time here now ;)


Try to call this method instead just to test it once more:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetDate() As String
    Return Date.Now.ToString()
End Function

Replace URL with this:

url: "/VerifMain.aspx/GetDate",


ASP.NET AJAX modified the JSON returned in 3.5. You need to access the d property, see http://encosia.com/never-worry-about-asp-net-ajaxs-d-again. I don't know what you're error is, but you'll see it if you changed the code to what's below:

$(document).ready(function () {

//menu()
$("#btnImprimer").click(function () {
    $.ajax({
        type: "POST",
            url: "/VerifMain.aspx/Lol",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert("Good"); 
            },
            error: function (data) {
                alert(data.d);
            }
        });
    });
});


Just examine the msg:

error: function (msg) {
                var i,s="";
                for(i in msg) s += (s?"\n":"") + i + ": " + msg[i];
                alert(s);
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜