Help with JQuery .ajax() post

Posted on 2009-12-23
Last Modified: 2012-05-08
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.



            type: "GET",

            url: "ChangeCodeAssignment2.aspx/GetNextRow", //Ajax file


            function(XMLHttpRequest) {

                //alert(this); // the options for this ajax request



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


                row = 0;


            error: function(XMLHttpRequest, textStatus, errorThrown) {

                // typically only one of textStatus or errorThrown 

                // will have info



                row = 0;


            //How you want the data formated when it is returned from the server.

            datatype: "json"





    public static string GetNextRow(string r)


        return "something " + r;


Open in new window

Question by:GeoffSutton
    LVL 10

    Author Comment

    Here is the calling webmethod with JQuery URL I mentioned.
    LVL 58

    Accepted Solution

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

    Open in new window

    LVL 10

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Title # Comments Views Activity
    How to count json data 2 41
    Datatables Select All from Filtered Rows 2 32
    SharePoint JSOM error 7 32
    JavaScript - Chessboard 36 31
    Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
    There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now