• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

Help with JQuery .ajax() post

Hey experts,

I'm attempting to make a jquery post to a page, and I seem to be getting nowhere with it.  I can't even find error messages or indicators of where I am going wrong.  I have been given a URL for using JQUery with ASP.NET webmethods, but am needing help with the JQuery itself, which I am unfamiliar with.

Please see the code below, thanks.

Geoff
$.ajax(
        {
            type: "GET",
            url: "ChangeCodeAssignment2.aspx/GetNextRow", //Ajax file

            beforeSend:
            function(XMLHttpRequest) {
                //alert(this); // the options for this ajax request
            },

            complete:
function(XMLHttpRequest, textStatus) {
    alert(textStatus); // the options for this ajax request
    row = 0;
},

            data: { r: row },  // create an object will all values
            //function that is called when server returns a value.
            success: function(data) {
                alert(data.returnValue);
                row = 0;
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown 
                // will have info

                alert("test2");
                alert("test1");
                row = 0;
            },
            //How you want the data formated when it is returned from the server.
            datatype: "json"
        }
        )


WEBMETHOD:
    [WebMethod]
    public static string GetNextRow(string r)
    {
        return "something " + r;
    }

Open in new window

0
GeoffSutton
Asked:
GeoffSutton
  • 2
1 Solution
 
GeoffSuttonAuthor Commented:
Here is the calling webmethod with JQuery URL I mentioned.
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
0
 
amit_gCommented:
Here are couple of things

- The method should be POST.
- The contentType must be specified as application/json; charset=utf-8.
- The variable row is undefined.
- Check the responseText to know if server threw any error/exception.

Attached is a working version
    <script language="javascript" type="text/javascript">
        var row = 999;

        $(document).ready(function() {
            $("#Result").click(function() {
                $.ajax({
                    type: "POST",
                    url: "ChangeCodeAssignment2.aspx/GetNextRow",
                    data: "{ r: " + row + " }",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                        $("#Result").append("<br>:" + data.d);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus + "\n" + errorThrown + "\n" + XMLHttpRequest.responseText);
                    }
                });
            });
        });
    </script>

Open in new window

0
 
GeoffSuttonAuthor Commented:
Thanks.  Row was originally defined in a while loop.  I will try these suggestions (on Tuesday) and hopefully get it working (even though another solution was found to speed the page load, and this will not be needed now) - I would like to get this worked out because loading a table x rows at a time could be a very useful and impressive bit of code to add to the library.
I'll let you know how it works out.
Geoff
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now