myroman
myroman

Reputation: 591

jQuery ajax cannot call WebMethod

My problem seems to have very simple solution and after searching on the SO and googling I couldn't find it.

I've got a client script which is located in UrlUpdatePage.aspx. It's periodically making a call to get json from aspx to get current status of the operation and show message to user. Below is ajax call:

  var listenForStatusChanges = function () {
      $.ajax({
        type: 'GET',
        url: "/UrlUpdatePage.aspx/GetRunningState",
        data: "{}",
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',

        success: function (data) {
          alert(data.IsRunning);
        }
      });
    };

In code-behind file UrlUpdatePage.cs I have such a method

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string GetRunningState()
{
    ...
    return jsonObj;
}

The problem is when the ajax call is made, the GetRunningStatemethod isn't triggered in debug mode and success callback isn't as well. Moreover, when I look at the details of the request in Firebug, the request is made but from the response tab I can see the call returns html markup, not the json data.

What should be done to expose GetRunningState method to the client and get the json from the server? I use .NET 4

Upvotes: 0

Views: 3522

Answers (2)

myroman
myroman

Reputation: 591

I figured out why I get html markup instead of json: due to security reasons UrlUpdatePage.aspx/GetRunningState is only callable via HTTP POST request. So, I need to use POST verb in two places: type: 'POST' and UseHttpGet = false.

Thank you all for help!

Upvotes: 1

anmarti
anmarti

Reputation: 5143

Try to remove the first slash from the URL. Like that:

url: "UrlUpdatePage.aspx/GetRunningState",

Upvotes: 0

Related Questions