Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to call a json webmethod (present in the code behind file) from browser. But get no output!

In my json.aspx.cs page: the web method is

[System.Web.Script.Services.ScriptService]
        public class _default : System.Web.Services.WebService
        {
            [WebMethod]
            [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
            public string[] UserDetails()
            {
                return new string[] { "abc", "def" };
            }
        }

When I try the following url from browser:

http://www.mydomain/json.aspx/UserDetails

I get no result! (The page is blank) I expect - the browser to show - { "abc", "def" }

Correct me if I am doing something wrong. My intention is to get the result as plain text in browser. Is it not possible to achieve if I place the webmethod in the code behind? I don't want to create a separate service for this...

share|improve this question

Is _default really the name of the class of is that just a mistake in the example? Should it not be json?

    [System.Web.Script.Services.ScriptService]
    public class Json : System.Web.Services.WebService
    {
share|improve this answer

You need a script manager on the page:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>

Then you can call your function like this:

// hook this up to a button click
function Test()
{
    PageMethods.UserDetails(Success);
}

// this is the callback from the page
function Success(Data)
{
    alert(Data);
}
share|improve this answer
up vote 0 down vote accepted

Actually none of the above worked.

Now instead of web form-code behind, I created a new asmx page & put it in the code behind Now, it shown some config issues: To solve it, I added:

<system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>

............

in thw eb config file.

Now I am getting the output in the browser.

The only thing remaining now - is I want it as plain text (json).

But I get it as xml:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<string>abc</string>
<string>def</string>
</ArrayOfString>

I am looking to fix this also.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.