1

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 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 ;)

1
  • 1
    you can post the solution you found as an answer to your question an mark it as the accepted answer! :) Commented May 30, 2011 at 15:24

3 Answers 3

2

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",
Sign up to request clarification or add additional context in comments.

Comments

1

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);
            }
        });
    });
});

Comments

0

Just examine the msg:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.