2

I just need to run a method when the page is finished loading.

Here's the js i'm using:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: document.URL + "/LoadAssignees"
        });
    });
</script>

And here's the method that i'm attempting to run:

[WebMethod()]
protected void LoadAssignees()
{
    //load assignees
    var sw = Stopwatch.StartNew();
    sw.Start();
    cboAssignees.DataSource = Uti.GetAssigneeList(_currentUser, 1, false);
    sw.Stop();
    cboAssignees.DataTextField = "Name";
    cboAssignees.DataValueField = "Guid";
    cboAssignees.DataBind();

    Response.Write(string.Format("Assignee load time: {0}", sw.ElapsedMilliseconds));
}

I felt like i was doing this right. I'm not getting any errors, but nothing is happening. I set a breakpoint on the method and it isn't getting hit, so I'm guessing my ajax call is incorrect. What am I missing?

On a side note, what I'm trying to do is run a method asynchronously as soon as the page is done rendering. If you can suggest a better way to accomplish that, that'll work for me as well :) thanks.

Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176
  • `document.URL` is the full browser path, not just the domain. Unless your on the root of the website `http://example.com` you're not going to get the URL your probably expecting. – travis Aug 08 '12 at 20:18
  • i did an alert(document.URL + "/LoadAssignees"); and it appears to be the correct path. – Sinaesthetic Aug 08 '12 at 20:19
  • What browser are you testing this in? In IE8+, Chrome and Firefox (with Firebug installed), you can view the request and response from your ajax call. Do that and see what kind of response you're getting. Sounds like you're going to be getting a 404 error, maybe a 500 server error. Get the error information and post it in here. – Gromer Aug 08 '12 at 20:20
  • Also, since you used Stopwatch.StartNew(), you don't need to call sw.Start() on the next line. StartNew initializes a Stopwatch and starts it for you. – Gromer Aug 08 '12 at 20:21
  • Aaaaaaand another comment... might want to try marking that WebMethod as public. I haven't used WebMethod in a while, forgot what you needed to use for the access modifier, but public seems to make more sense. – Gromer Aug 08 '12 at 20:23
  • 2
    you know what i think its supposed to be public static – Sinaesthetic Aug 08 '12 at 20:23
  • It is, I put the MS documentation on it in my answer :) – Gromer Aug 08 '12 at 20:25
  • ok well with that being said, now this whole thing wont work. since the method is static, i cant load the list into the dropdownlist D: – Sinaesthetic Aug 08 '12 at 20:26
  • That may be true, but my answer is correct for this question. You're going to need to do the binding a different way, which should be a whole new question in itself. Edit: why are you calling this method as soon as the page is rendered? Do you call it other times? If you only call it that first time, why not move the code into the PageLoad for the page? – Gromer Aug 08 '12 at 20:40
  • because moving it to page load prevents it from displaying the page before the method is complete. I want to display the page while some of the lists finish populating – Sinaesthetic Aug 08 '12 at 21:08
  • @Sinaesthetic, check this post for some help on a different way to populate your list: http://stackoverflow.com/questions/2651091/jquery-ajax-call-to-httpget-webmethod-c-not-working. Basically, return json from your WebMethod, then in the success callback on your ajax call, fill up your dropdown with javascript and the data you got back. – Gromer Aug 08 '12 at 21:14

2 Answers2

1

Sinaesthetic,

You need to handle the response in your $.ajax() function call. In your case I would use the "success" parameter that you can find described in the jQuery.ajax() description on jquery.com.

This is because the Response.Write method in your code behind does not write back to the page you called it from, but to the ajax function itself. If that response is not handled, you get nothing.

So, for example:

    $.ajax({
        type: "GET",
        url: document.URL + "/LoadAssignees",
        success: function(data) {
            alert(data);
        }
    });

EDIT

You should also maybe try a different method of setting the URL for your web method in the Ajax call: if your page's url is http://mydomain.com/dir1/pagex.aspx, then your ajax will be trying to contact http://mydomain.com/dir1/pagex.aspx/LoadAssignees

Hope that helps!

  • Matt
Matt
  • 1,213
  • 14
  • 39
1

Yeah, looks like you're access modifier is too restrictive:

Methods within a class that have this attribute set are called XML Web service methods. The method and class must be public and running inside an ASP.NET Web application.

That's from the 'Remarks' section here: http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.aspx

Gromer
  • 9,861
  • 4
  • 34
  • 55