1

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

2
  • copy the response u get as html into a notepad, save it as a html file, and open it. and voila! there's your error. next post that screenshot here. Commented Jun 25, 2013 at 14:21
  • @passionateCoder I cannot upload screenshots but there's no point in it: the response shows just the markup of this page. Commented Jun 25, 2013 at 14:28

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

I admit it works with GET in the test project but not in one I referred to :( So the solution may lay in Web.config but I don't know where
0

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

url: "UrlUpdatePage.aspx/GetRunningState",

1 Comment

Try to remove the scripmethod annotation. I did it many times and it worked successfully out of the box, just settin the webmethod anotation. Don't forget to compile server side code ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.